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

Notices

Rate this Entry

QT4 Interactive: qt4-info utility to list methods and properties

Posted 05-29-2012 at 09:31 AM by rainbowsally
Updated 06-01-2012 at 12:12 PM by rainbowsally (correction in error msg)

CHANGELOG: 6/1/2001
Corrected argv[<value>] in error msg at line 102 of file: src/qt4-info.cpp.

[If you work with qt4, this is well worth the exercise. You might be surprised at how much we do with so little code. -rs]

Care for a bit of Ye Olde Computer Mad Science? This one is kind of fun.

Todays Features:
  • Look up all QT4 classes and list properties or meta object method calls.

[This is part of the QT Interactive project, because it uses the QT system itself (i.e., the libs, not the docs) to generate the information in a format that runs quickly, and displays just enough to jog your memory or pique your curisoty, in which case the docs can be checked for greater detail when needed.]

To take a look at the collection of classes we can examine, check out the file src/widgetlist.loader at the bottom.

This is a console app, but could easily be beefed up with qt front end but that's not done here (yet) for the sake of the sizes.

If you're using mc2, the non-defaults are listed at the top of the qt4-info.cpp file. If you have another makefile generator, you're on your own. ;-)

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

/* mc2.def non-defaults
 * --------------------
 * OUTNAME = qt4-info
 * COMPILE = g++ -c 
 * CFLAGS = -Wall -W -O2 #
 * INCLUDE = -I$(SRCDIR) -I/usr/include -I/usr/include/Qt -I/usr/include/QtGui -I /usr/include/QtCore 
 * LINK = g++ 
 * LDFLAGS = -lQtGui -lQtCore 
 */

#include <QApplication>
#include <QtCore/qmetaobject.h>
#include <stdio.h>
#include "includeslist.h"

void dbg(){}

const char* lq_widgetlist[] = 
{
  #define MACFN(x) #x,
  #include "widgetlist.loader"
  #undef MACFN
  0
};

enum
{
  #define MACFN(x) ID_##x,
  #include "widgetlist.loader"
  #undef MACFN
};

// returns -1 if not found
int lq_widget_id(const char* name)
{
  const char** tbl = lq_widgetlist;
  for(int i = 0; tbl[i] != 0; i++)
  {
    if(0 == strcasecmp(tbl[i], name))
      return i;
  }
  return -1;
}

const QMetaObject* lqGetMetaObject(const char* name)
{
  QWidget* t;
  int id = lq_widget_id(name);
  if(id < 0)
    return 0;
  switch(id)
  {
    #define MACFN(x) case ID_##x: t = new x; break;
    #include "widgetlist.loader"
    #undef MACFN
    default:
      return 0; // already done above, but ok
  }
  return t->metaObject();
}

int usage(int errcode);

#define STREQ(a, b) (0 == strcasecmp(a, b))

int main(int argc, char **argv)
{
  dbg();
  QApplication app(argc, argv);
  const QMetaObject* m;
  int optype; // methods or properties lookup.
  
  if(argc < 2)
    return usage(1);
  if(argc > 3)
    return usage(1);
  
  if(STREQ(argv[1], "--help"))
    return usage(0);
  else if (STREQ(argv[1], "-m"))
    optype = 1; // methods
    else if (STREQ(argv[1], "-p"))
      optype = 2; // properties
      else if (STREQ(argv[1], "-list"))
      {
        const char** tbl = lq_widgetlist;
        for(int i = 0; tbl[i] != 0; i++)
          printf("%s\n", tbl[i]);
        printf("\n");
        return 0;
      }
      else
        return usage(1);       
      
      
      m = lqGetMetaObject(argv[2]);
      if(!m)
      {
        fprintf(stderr, "Can't get QMetaObject for %s\n", argv[2]); // was argv[1] -rs
        return 1;
      }
      int cnt = m->methodCount();
      
      if(optype == 1) // methods
  while(m)
  {
    QStringList properties;
    QStringList types;
    
    printf("\nFrom class: %s\n", m->d.stringdata);
    printf("---------------------------\n");
    
    for(int i = m->methodOffset(); i < m->methodCount(); i++)
    {
      QMetaMethod a = m->method(i);
      
      properties << QString::fromLatin1(m->method(i).signature());
      properties << QString::fromLatin1(m->method(i).typeName());
    }
    
    int odd = 0;
    foreach(QString s, properties)
    {
      const char* str = s.toAscii();
      if(!odd)
        printf("%s : ", str);
      else
        printf("%s\n", *str == 0 ? "void" : str);
      odd = !odd;
    }
    cnt = m->methodOffset();      
    //    cnt = m->methodOffset();      
    m = m->d.superdata;
  }
  else
    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;
}

int usage(int errcode)
{
  #include "usage_str.dat"
  if(errcode)
    fprintf(stderr, usage_str);
  else
    printf(usage_str);
  return errcode;
}
file: src/includeslist.h
Code:
#include <QCalendarWidget>
#include <QCheckBox>
#include <QColorDialog>
#include <QColumnView>
#include <QComboBox>
#include <QCommandLinkButton>
#include <QDateEdit>
#include <QDateTimeEdit>
#include <QDesktopWidget>
#include <QDial>
#include <QDialog>
#include <QDialogButtonBox>
#include <QDockWidget>
#include <QDoubleSpinBox>
#include <QErrorMessage>
#include <QFileDialog>
#include <QFocusFrame>
#include <QFontComboBox>
#include <QFontDialog>
#include <QFrame>
#include <QGenericMatrix>
#include <QGraphicsView>
#include <QGroupBox>
#include <QLabel>
#include <QLineEdit>
#include <QListWidget>
#include <QMainWindow>
#include <QMdiArea>
#include <QMdiSubWindow>
#include <QMenu>
#include <QMenuBar>
#include <QMessageBox>
#include <QPageSetupDialog>
#include <QPlainTextEdit>
#include <QPrintDialog>
#include <QPrintPreviewDialog>
#include <QPrintPreviewWidget>
#include <QProgressBar>
#include <QProgressDialog>
#include <QPushButton>
#include <QRadioButton>
#include <QScrollArea>
#include <QScrollBar>
#include <QSlider>
#include <QSpinBox>
#include <QSplashScreen>
#include <QSplitter>
#include <QStackedWidget>
#include <QStatusBar>
#include <QTabBar>
#include <QTableView>
#include <QTableWidget>
#include <QTabWidget>
#include <QTextBrowser>
#include <QTextEdit>
#include <QTimeEdit>
#include <QToolBar>
#include <QToolBox>
#include <QToolButton>
#include <QTreeView>
#include <QTreeWidget>
#include <QUndoView>
#include <QWidget>
#include <QWizard>
#include <QWizardPage>
#include <QWMatrix>
#include <QWorkspace>
#include <QX11EmbedContainer>
#include <QX11EmbedWidget>
file: src/usage_str.dat
Code:
/* usage_str.txt converted with txt2cstr */
const char* usage_str =
"\n"
"Usage: qt4-info -list | [-m|-p|] <QClassName>\n"
"\n"
"  -list             prints out a list of all available classes\n"
"\n"
"  -m QClassName     prints out all the callable meta object methods\n"
"\n"
"  -p QClassName     prints out all the properties\n"
"  \n"
"  Methods and properties are shown heirarchically as inherited down \n"
"  to QObject.\n"
;
file: src/widgetlist.loader
Code:
MACFN(QCalendarWidget)
MACFN(QCheckBox)
MACFN(QColorDialog)
MACFN(QColumnView)
MACFN(QComboBox)
MACFN(QCommandLinkButton)
MACFN(QDateEdit)
MACFN(QDateTimeEdit)
MACFN(QDesktopWidget)
MACFN(QDial)
MACFN(QDialog)
MACFN(QDialogButtonBox)
MACFN(QDockWidget)
MACFN(QDoubleSpinBox)
MACFN(QErrorMessage)
MACFN(QFileDialog)
MACFN(QFocusFrame)
MACFN(QFontComboBox)
MACFN(QFontDialog)
MACFN(QFrame)
MACFN(QGraphicsView)
MACFN(QGroupBox)
//  MACFN(QInputDialog)
MACFN(QLabel)
MACFN(QLineEdit)
MACFN(QListWidget)
MACFN(QMainWindow)
MACFN(QMdiArea)
MACFN(QMdiSubWindow)
MACFN(QMenu)
MACFN(QMenuBar)
MACFN(QMessageBox)
MACFN(QPageSetupDialog)
MACFN(QPlainTextEdit)
MACFN(QPrintDialog)
MACFN(QPrintPreviewDialog)
MACFN(QPrintPreviewWidget)
MACFN(QProgressBar)
MACFN(QProgressDialog)
MACFN(QPushButton)
MACFN(QRadioButton)
MACFN(QScrollArea)
MACFN(QScrollBar)
MACFN(QSlider)
MACFN(QSpinBox)
MACFN(QSplashScreen)
MACFN(QSplitter)
MACFN(QStackedWidget)
MACFN(QStatusBar)
MACFN(QTabBar)
MACFN(QTableView)
MACFN(QTableWidget)
MACFN(QTabWidget)
MACFN(QTextBrowser)
MACFN(QTextEdit)
MACFN(QTimeEdit)
MACFN(QToolBar)
MACFN(QToolBox)
MACFN(QToolButton)
MACFN(QTreeView)
MACFN(QTreeWidget)
MACFN(QUndoView)
MACFN(QWidget)
MACFN(QWizard)
MACFN(QWizardPage)
MACFN(QWorkspace)
MACFN(QX11EmbedContainer)
MACFN(QX11EmbedWidget)
TROUBLESHOOTING:

If you are missing any of the classes in this utility (not to likely but possible) you can remove the header from qt4-info.cpp and the item from the widgetlist.loader (see QInputDialog in the widgetlist.loader above) and recompile.


Recommended installation location:

Put this file under ~/bin/src/LQ-projects/ in a new subdirectory named "qt4-info" (same as the executable, not a prob) and make a symlink to the executable in your ~/bin folder so it's immediately set in your PATH. (E.g. new.symlink qt4-info ~/bin, done inside the qt4-info subdir after the qt4-info executable has been compiled.)

If you want a simple symlink-based installer utility to make this kind of activity super-easy, see early posts in this blog labeled KDE utils. Not all of those require KDE, including the 'new.symlink' utility.

Placing qt4-info and the sources in your home sandbox and symlinking the executable into the PATH makes it easy to check out other classes that you may have that I don't have and add them to your app, or you can do whatever other modifications you see fit and the changes will be effective immediately.

The Computer Mad Science Team

:-)
Posted in Uncategorized
Views 668 Comments 0
« Prev     Main     Next »
Total Comments 0

Comments

 

  



All times are GMT -5. The time now is 12:20 AM.

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