Qt Designer: adding TQtWidget

Hi,

I would like to use the Qt Designer to add a TCanvas, but
does one add TQtWidget to the Qt Designer V4.3 ?

Cheers
Anyes

Ps: I’m using Root 5.17.06

TQtWidget class is a subclass of QWidget class.
That means what you are asking about is how to use the custom Qt widget with the Qt4 designer. The ROOT Manual does provide the guidance for Qt 3 designer. That doesn’t explain yet how to use one from the Qt4 distribution.
Fortunately, using Qt4 designer is much simpler. Qt4 does not require any special “Custom Widget Definition” file to be provided.

Under Qt4 one should use the regular “widget” and “promote” it to TQtWidget (see
doc.trolltech.com/4.3/designer-u … ng-widgets )

I have paraphrased the original Qt paragraph to read it as follows (Hope the TrollTech will forgive my plagiarism)

[quote]One can use ROOT TCanvas embedded into the TQtWidget class.
However TQtWidget class is unavailable to the designer. In such case one can substitute similar widgets to represent the missing widgets. For example, we might represent instances of a embedded TCanvas, TQtWidget, with instances of QWidget and promote these to TQtWidget so that uic generates suitable code for this missing class.
. . .
To add a placeholder, select an object of a QWidget class and choose Promote to… from the form’s context menu. After entering the class name, TQtWidget, and header file, TQtWidget.h, in the lower part of the dialog, choose Add. The placeholder class will now appear along with the base class in the upper list. Click the Promote button to accept this choice.
Now, when the form’s context menu is opened over objects of the base class, the placeholder class will appear in the Promote to submenu, allowing for convenient promotion of objects to TQtWidget[/quote]

Follow this example, save and compile your new project.

I am on shift right now. I’ll create and upload the short example as soon as my shift is over, i.e. tomorrow.

Please find the Qt4 project attached.
To build the example

  1. untar
  2. qmake
  3. make

Enjoy.
ex1.qt4.tar.gz (5.55 KB)

Please, find another very small Qt4-based example that can be used to build the GUI with the Qt4 designer. It is not optimal though, it is to show the idea. For example, it reads all objects from the ROOT file “right away”. The real application may want to delay reading.

[code]QStandardItem *CreateItem(TDirectory *parentDir)
{
// Create tree view of the ROOT TDirectory
TDirectory *saveDir = (parentDir == gDirectory) ? 0 : gDirectory;
if (saveDir) parentDir->cd();

QStandardItem *item = 0;
item = new QStandardItem(parentDir->GetName());
item->setData(qVariantFromValue((void *)parentDir));
item->setIcon(TQtObjectViewFrame::Icon(QFileIconProvider::Folder));

TList *listOfKeys = parentDir->GetListOfKeys();
TIter next(listOfKeys);
TObject *key = 0;
while ((key = next())) {
TObject *obj = ((TKey *)key)->ReadObj();
QStandardItem *nextItem = 0; TDirectory nextDir = 0;
if ( ( nextDir = dynamic_cast<TDirectory
>(obj)) ) {
nextItem = CreateItem(nextDir);
} else {
nextItem = new QStandardItem(obj->GetName());
}
nextItem->setData(qVariantFromValue((void *)obj));
QList<QStandardItem *> columns;
columns << nextItem
<< new QStandardItem(obj->ClassName())
<< new QStandardItem(obj->GetTitle());
item->appendRow(columns);
if (!nextDir) {
if (obj->InheritsFrom(TH3::Class()))
nextItem->setIcon(QIcon(“h3_s.xpm”));
else if (obj->InheritsFrom(TH2::Class()))
nextItem->setIcon(QIcon(“h2_s.xpm”));
else if (obj->InheritsFrom(TH1::Class()))
nextItem->setIcon(QIcon(“h1_s.xpm”));
else if (obj->InheritsFrom(“TNtuple”))
nextItem->setIcon(QIcon(“ntuple_s.xpm”));
else if (obj->InheritsFrom(“TTree”))
nextItem->setIcon(QIcon(“tree_s.xpm”));
else
nextItem->setIcon(TQtObjectViewFrame::Icon(QFileIconProvider::File));
}
}
if (saveDir) saveDir->cd();
return item;
}[/code]
The attached Qt project is to build the TQtObjectViewFrame class which is a subclass of QTreeView widget. (see: doc.trolltech.com/4.4/qtreeview.html )
The class objects emits Activated(TObject*) Qt signal. That can be used to incorporate the widget into the more complex GUI application.
The widget can be used with the Qt designer by promoting the Qt4 QTreeView class (see: doc.trolltech.com/4.4/designer-u … ng-widgets )

To build the and run the test application do

  1. “Un-tar” the archive tar -xzvf HelloObjectTree.tar.gz
    2 Invoke: qmake make
  2. Usage: Usage: HelloObjectTree [-h | [ [-style=[windows | platinum | cgi | kde ] | [myRootfile.root]]The application creates an instance of the widget and populates it with the objects read from the ROOT file . It pops up the “Open File Dialog” as needed (see: doc.trolltech.com/4.3/qfiledialo … enFileName )


    HelloObjectTree.tar.gz (5.51 KB)

This is a last :unamused: program for this thread. It combines the two previous examples.

[quote]“HelloFileBrowser” is another small Qt4 application built with Qt4 designer.
It shows how to use two custom widgets:

  1. “TQtObjectViewFrame” derived from QTreeView widget to represent the TFile structure
    (see: doc.trolltech.com/4.3/qtreeview.html )

  2. “TQtWidget” derived from QWidget to represent the embedded TCanvas

The GUI of the example “promotes” the standard Qt4 widgets to the custom TQtObjectViewFrame and TQtWidget
(see: doc.trolltech.com/4.3/designer-u … ng-widgets )

To build and run the test application do

  1. “Un-tar” the archive
       tar -xzvf HelloFileBrowser.tar.gz

2 Invoke:

qmake make
3. Usage:

      Usage: HelloFileBrowser  [-h | [ -style=[windows | platinum | cgi | kde ]
    The application creates an instance of the widget and populates it with
    the objects read from the ROOT file . It pops up the "Open File Dialog" as
    needed
    (see: [doc.trolltech.com/4.3/qfiledialo ... enFileName](http://doc.trolltech.com/4.3/qfiledialog.html#getOpenFileName) )

4. To change the GUI invoke the Qt4 designer:
   designer HelloFileBrowser.ui

HelloFileBrowser.tar.gz (8.05 KB)

Anyes Taffard has reported he can not compile the last example against of the last ROOT version.

I would like to thank him for his report. The patch below explains and fixes the issue.

[code]Index: HelloFileBrowser.cxx

RCS file: /data01/CVS/root/qtExamples/Qt4/HelloFileBrowser/HelloFileBrowser.cxx,v
retrieving revision 1.1
diff -u -u -w -b -r1.1 HelloFileBrowser.cxx
— HelloFileBrowser.cxx 2 Jan 2008 21:09:28 -0000 1.1
+++ HelloFileBrowser.cxx 4 Feb 2008 16:00:18 -0000
@@ -6,6 +6,7 @@
#include
#include “TH1.h”
#include “TH2.h”
+#include “TROOT.h”

#include “TFile.h”
#include “TKey.h”
Index: TQtDirectoryBrowser.cxx

RCS file: /data01/CVS/root/qtExamples/Qt4/HelloFileBrowser/TQtDirectoryBrowser.cxx,v
retrieving revision 1.1
diff -u -u -w -b -r1.1 TQtDirectoryBrowser.cxx
— TQtDirectoryBrowser.cxx 2 Jan 2008 21:09:28 -0000 1.1
+++ TQtDirectoryBrowser.cxx 4 Feb 2008 16:00:18 -0000
@@ -6,6 +6,7 @@
#include “TClass.h”
#include “TDataMember.h”
#include “TRealData.h”
+#include “TDirectory.h”
#include “TKey.h”
#include “TH3.h”
#include [/code]
HelloFileBrowser.tar.gz (8.23 KB)