Qt Designer - The Single Inheritance Approach
-
I'm following a tutorial that showed me how to create a GUI 'Simple Dialog Example' with a list box, text box and three buttons - Add,
Remove and Ok. I've already sorted out a few issues and ma now stumped trying to get the Single Inheritance Approach to work. I
finally determined that the class definition belonged in its own .h file and the constructor belongs in my .cpp module along with the main() used in the previous exercise - The Direct Approach.My question is: How does one now get the thing to display? Since I can no longer invoke ui.setupUi(window); from main() and the
tutorial claims that ui.setupUi(this) now does all of the setup for the dialog in the ocnstructor, what does one need to do to trigger
its display on the terminal screen? What gets processed before window->show()? so there's something to displayThx!
-
The ui.setupUi(), or ui->setupUi(), call belongs in the constructor of the widget class not in main().
You can see it under "Using a Designer UI File in Your Application":http://qt-project.org/doc/qt-4.8/designer-using-a-ui-file.html#the-single-inheritance-approach in the Designer documentation.
-
Yes. Thank you. I can see that. The same recommendation appears in the tutorial on which I am working. I already have code similar to that implemented. My question is what does one use to trigger that so the dialog will pop up on the screen? Simply declaring some variable of the ImageDialog class and referencing that variable causes the constructor to be invoked. Great.
What is required to get the initialized object to appear? Does one use window->show() again? If so
what occurs before that?Does the tutorial assume that by merely executing the constructor the dialog will appear on one's
display? That's not the case.I am also using QtCreator and walking through debug sessions trying to get this to work - to no
avail.Frustrating?
my.h
@#include <QApplication>
#include "ui_imagedialog.h"class ImageDialog : public QDialog
{
Q_OBJECTpublic: ImageDialog(QWidget *parent = 0); private: Ui::ImageDialog ui;
};
@main.cpp
@
#include <QApplication>
//#include "ui_imagedialog.h"
#include "my.h"#if 1
ImageDialog::ImageDialog(QWidget *parent) : QDialog(parent)
{
ui.setupUi(this);connect(ui.pbOk, SIGNAL(clicked()), this, SLOT(accept()));
}
#endifint main(int argc, char *argv[])
{
QApplication app(argc, argv);
QDialog *window = new QDialog;// Ui::ImageDialog ui;
// ui.setupUi(window);window->show(); return app.exec();
}
@ -
Any QWidget (include QDialog) is displayed by calling show(), although with a dialog, you'll want to use exec() if it's modal.
On your line 17 above, you are instantiating a simple QDialog. You want to instantiate an ImageDialog, so your code should be:
@
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
ImageDialog *window = new ImageDialog;window->exec(); // Edited (Originally had "window.exec()") return app.exec();
}
@Also, I would break the ImageDialog code into image_dialog.h and image_dialog.cpp, rather than including the ImageDialog class code in main.cpp. If not, you'll have to add @ #include "main.moc" @ to your main.cpp file (if you haven't already) to add any autogenerated moc code that supports your ImageDialog class.
-
Wow! Thank you. I had been trying a number of variable declarations for the ImageDialog class/type
and had not yet stumbled on window, though I knew there was some relationship between the two.I took your recommendation and moved my constructor code to its own module. It now pops up the
dialog.I did, however, receive a complaint from the compiler over the use of:
window.exec()
stating, "request for member 'exec' in 'window', which is of non-class type 'ImageDialog*'
I commented it out and used window->show(); as I'd previously done.
I was impressed with Qt Creator's ability to switch windows back and forth with my Qt Designer
session - although I seem to have lost that after having run a few dozen debug sessions... -
Oh, that should have been @window->exec();@ not @window.exec();@ My bad.
The error message you were getting was regarding the -> vs . (not exec() vs show())
bq. I had been trying a number of variable declarations for the ImageDialog class/type
and had not yet stumbled on window, though I knew there was some relationship between the two.I'm not quite sure I understand what you're saying here. The variable name window has no bearing on anything. You could just as easily have used anything else.