PDA

View Full Version : Widgets not displaying on dialog box



te777
1st November 2013, 00:36
I'm using a dialog box created in Qt Creator, class called Ui_DIalog. The dialog displays when I use it but the window title is just the name of my project, not the designed window title, and the widgets don't show: 6 labels, a lineEdit , and OK and cancel buttons. None of them display. The dialog is called within another class member procedure. The code is:


Ui_Dialog D1;

if(D1.exec() == QDialog::Accepted)
{ // some code }

Any help with this would be appreciated. Thanks in advance.

Ginsengelf
1st November 2013, 10:42
Hi, you don't use the classes created by Qt Designer and uic directly. Instead you use them as a member or base class of your own widget class.
The widgets will be shown after a call to setupUi() method.

Ginsengelf

edit: maybe this helps: http://qt-project.org/doc/qt-4.8/designer-using-a-ui-file.html

te777
1st November 2013, 13:17
I got the widgets to display. When I originally created the dialog in the designer, the dialog was was named by Object

as Dialog. But no dialog.h was created. So I used uic in the Qt bin folder to create it. In the resulting dialog.h the

class is called Ui_Dialog, with a namespace declaration at the bottom of it called Ui, specifying the Dialog class.

So in my class .cpp that's calling the dialog, I added:

using namespace Ui;

Then in my class calling procedure I used the following code:


Ui::Dialog ui_d1;
Dialog D1;
ui_d1.setupUi(&D1);

if(D1.exec() == QDialog::Accepted)
{

Temp1 = D1.lineEdit->text();

// Some following code

}

But now the program crashes when using the text() function to return the text of the lineEdit. Temp1 is a QString

declared variable and the program runs when I use a valid value of "2", commenting out the lineEdit->text() line.

Any ideas what could be causing that?

anda_skoa
1st November 2013, 19:26
this is a very uncommon way to apply the generated code to a dialog.

Usually the generated code (Ui::Dialog in your case) is instantiated in the constructor of the class (in your case Dialog) and then setupUi is called with "this".

You problem is very likely that you have a lineEdit member in your Dialog class that is not initialized, maybe not even created.
You see the UI element of Ui::Dialog, but you are not accessing them in the if's body.

Proper encapsulation can easily avoid those inconsistencies.

Cheers,
_

te777
2nd November 2013, 02:29
Just adding

setupUI (this);

to the dialog constructor fixed the problem, along with just using the following declaration in my class procedure:

Dialog *D1 = new Dialog;

Thanks for the replies.