LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Blogs > rainbowsally
User Name
Password

Notices

Rate this Entry

QT4 Property dump example. (simple terminal app)

Posted 04-30-2012 at 02:24 AM by rainbowsally

This code just prints out the properties (and where they come from) for one widget. You can see the dump around the middle of this blog entry.

A full blown version would require a LOT of includes and libs and a way to replace a "ClassName" string in the file with the actual class we want to dump. (See "%s" in three places below.)

file: src/qt4-props.cpp
Code:
// file: qt4-props.cpp - lists all properties for (in this case)
// a QPushButton class.

#include <QApplication>
#include <QtCore/qmetaobject.h>
#include <QPushButton>
#include <stdio.h>

int main(int argc, char **argv)
{
  QApplication app(argc, argv);
  
  QPushButton *t = new QPushButton();
  
  const QMetaObject* m;
  m = t->metaObject();
  int cnt = m->propertyCount();
  // int cnt = m->methodCount();
  
  while(m)
  {
    QStringList properties;
    QStringList types;
    
    printf("\nFrom class: %s\n", m->d.stringdata);
    printf("---------------------------\n");
    
    for(int i = m->propertyOffset(); i < m->propertyCount(); i++)
      // for(int i = m->methodOffset(); i < m->methodCount(); i++)
    {
      QMetaProperty a = m->property(i);
      // QMetaMethod a = m->method(i);
      
      properties << QString::fromLatin1(m->property(i).name());
      properties << QString::fromLatin1(m->property(i).typeName());
      // properties << QString::fromLatin1(m->method(i).signature());
    }
    
    int odd = 0;
    foreach(QString s, properties)
    {
      const char* str = s.toAscii();
      if(!odd)
        printf("%s : ", str);
      else
        printf("%s\n", str);
      odd = !odd;
    }
    cnt = m->propertyOffset();      
    //    cnt = m->methodOffset();      
    m = m->d.superdata;
  }
  return 0;
}

/*
 # file: m*c2.def - type 'mc2 -i' or 'mc2 -u' to init a new makefile
 
 OUTNAME = qt4-props
 BINDIR = .
 SRCDIR = src
 OBJDIR = o
 COMPILE = g++ -c 
 CFLAGS = $(DEFINES) -Wall -W -O2 
 INCLUDE = -I ~/usr32/include -I$(SRCDIR) -I/usr/include -I/usr/include/Qt -I/usr/include/QtGui -I /usr/include/QtCore 
 
 # add these as needed to the includes above.  
 # -I /usr/include/Qt             -I /usr/include/QtHelp         -I /usr/include/QtSvg
 # -I /usr/include/Qt3Support     -I /usr/include/QtMultimedia   -I /usr/include/QtTest
 # -I /usr/include/QtCore         -I /usr/include/QtNetwork      -I /usr/include/QtUiTools
 # -I /usr/include/QtDBus         -I /usr/include/QtOpenGL       -I /usr/include/QtWebKit
 # -I /usr/include/QtDeclarative  -I /usr/include/QtScript       -I /usr/include/QtXml
 # -I /usr/include/QtDesigner     -I /usr/include/QtScriptTools  -I /usr/include/QtXmlPatterns
 # -I /usr/include/QtGui          -I /usr/include/QtSql
 
 LINK = g++ 
 LDFLAGS = -lLQ -lQtGui -lQtCore 
 
 # add as needed. see notes for DEFINES and INCLUDES above if you need to split lines
 # -lQt3Support              -lQtNetwork               -lQtCLucene
 # -lQtOpenGL                -lQtCore                  -lQtScript
 # -lQtDBus                  -lQtScriptTools           -lQtDeclarative
 # -lQtSql                   -lQtDesignerComponents    -lQtSvg
 # -lQtDesigner              -lQtTest                  -lQtGui 
 # -lQtWebKit                -lQtHelp                  -lQtXmlPatterns
 # -lQtMultimedia            -lQtXml
 
 
 # adjust the LIB paths for your system.  Try 'locate libQtGui'
 # to find the right path for your system.
 LIB = -L ~/usr32/lib -L/usr/lib 
 
 ################################################################
 ## User Defined Targets
 
 clean: 
 @rm -f $(MAIN)
 @rm -f $(OBJ)
 @rm -f `find . -name *~ -o -name *.*~`
 
*/

Here's the property dump for QPushButton using this utility.

Code:
$> qt4-props

From class: QPushButton
---------------------------
autoDefault : bool
default : bool
flat : bool

From class: QAbstractButton
---------------------------
text : QString
icon : QIcon
iconSize : QSize
shortcut : QKeySequence
checkable : bool
checked : bool
autoRepeat : bool
autoExclusive : bool
autoRepeatDelay : int
autoRepeatInterval : int
down : bool

From class: QWidget
---------------------------
modal : bool
windowModality : Qt::WindowModality
enabled : bool
geometry : QRect
frameGeometry : QRect
normalGeometry : QRect
x : int
y : int
pos : QPoint
frameSize : QSize
size : QSize
width : int
height : int
rect : QRect
childrenRect : QRect
childrenRegion : QRegion
sizePolicy : QSizePolicy
minimumSize : QSize
maximumSize : QSize
minimumWidth : int
minimumHeight : int
maximumWidth : int
maximumHeight : int
sizeIncrement : QSize
baseSize : QSize
palette : QPalette
font : QFont
cursor : QCursor
mouseTracking : bool
isActiveWindow : bool
focusPolicy : Qt::FocusPolicy
focus : bool
contextMenuPolicy : Qt::ContextMenuPolicy
updatesEnabled : bool
visible : bool
minimized : bool
maximized : bool
fullScreen : bool
sizeHint : QSize
minimumSizeHint : QSize
acceptDrops : bool
windowTitle : QString
windowIcon : QIcon
windowIconText : QString
windowOpacity : double
windowModified : bool
toolTip : QString
statusTip : QString
whatsThis : QString
accessibleName : QString
accessibleDescription : QString
layoutDirection : Qt::LayoutDirection
autoFillBackground : bool
styleSheet : QString
locale : QLocale
windowFilePath : QString
inputMethodHints : Qt::InputMethodHints

From class: QObject
---------------------------
objectName : QString

It's kind of interesting to see how we can parse the property inheritance heirarchy and this is probably similar to how we might get other info about QT classes somewhat interactively.

And with that in mind...

What would happen if we change "property" to "method" (matching upper/lower case as req'd) and dumped 'signature()' instead of 'name()' for each method?

Code:
base64 -d << _eof | gunzip > qt4-methods.cpp
H4sICKssnk8AA3F0NC1wcm9wcy5jcHAApVZbU+M2FH7XrziF3cEJ2GnZPgW6LSRA3XUS2MDQh31Y
YSuJdmXJSDIM0ym/vUe2k9iJw7TTPGSsc/nO7dOxez2YccH68Gh/9lNmFyoxQZxl4IPgxhqgQkAl
h5nS4HEJdsENxNSwDun1gMLNdW4W57m1SkIsqDEBIftcxiJPGJzenGWZ4DG1XMmPdbkdKM16j4hO
1cM3Fttg0dCvUetiYxOunCXh0kJKufTcA9Xz+AjiBdXQ7eLhqUP+IgD14ECzzCvtCoMT1DuTWvZd
C7+AZM91obc0jJU0Fm5GmO+kyLcLqVOl6GP9j+lKXnq4rGLpANNCiS0cqFzaFd7zAjvvpR18dKli
JlOruZxH2HjItMqYtpyZky2dfcmW4uIvQ42deXtf5KV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v4Kvlc9B9
/UpIt/cPquplspQLAAA=
_eof
;-)

[See 'mc2 -help all' re. Multiple Executables: How To. It's a lot easier than you probably think. Use page down or dump to stdout if you don't like the screwy xmessage scrollbars. Put both sources in the same source dir and both executables compile at once. Remember to 'update' your Makefile.]

.
Posted in Uncategorized
Views 421 Comments 0
« Prev     Main     Next »
Total Comments 0

Comments

 

  



All times are GMT -5. The time now is 01:20 PM.

Main Menu
Advertisement

My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration