PDA

View Full Version : Getting started with Qt Creator 4.6...



JesperWe
15th December 2009, 13:21
Hi all! I am a total noob with Qt, although I am a seasoned developer in many other environments. (Mostly Java I am afraid :p)

But I have decided to try to learn Qt since I have some spare time...

I have looked for, but failed to find, a walk-through of how to create some kind of "Hello World" application using Qt Creator + Qt Designer. There are various tutorials on the two tools separate, but they don't tell the full story of how to make them work together.

So I have downloaded and installed the 4.6.0 SDK, and created a first "Qt GUI Application" using the New Project wizard.

I am able to modify the UI and build and run the app without problems. Next step would be to have the UI actually perform some action.

So I created a "Help > About..." menu item and added a "triggered" action to it, which gave me an on_actionAbout_triggered() slot in my MainWindow class, and the corresponding method stub in the mainwindow.cpp. Fine.

Then I create a new Form of class QDialog which is to be the about box. Also simple. Fine.

If I run a new build I now get a new ui header file for the new About box. Great.

Except I can't seem to understand how I make the on_actionAbout_triggered()-method in the MainWindow class display the About box. I thought I should create a new instance of the QDialog that should have the MainWindow as parent, and then call .show() on this, or something similar.

But how, exactly?

It seems in the FormEditor window I have an object called AboutBox of class QDialog, but I can't seem to find any corresponding code that has been generated. The generated .h file has a class called UI::AboutBox but it is not a QDialog...

Unfortunately the "Writing a Simple Program with Qt Creator" part in the QtCreator manual only has one form, so it doesn't help me here.

/j

franz
15th December 2009, 18:31
You'll have to work with the Ui::AboutBox in the same fashion as the ui in your main window is handled. Probably you could then get away with something like


void MainWindow::on_thingy_triggered()
{
AboutBox *dialog = new AboutBox(this);
dialog->setModal(true); // you probably want your about box to be window modal
dialog->setAttribute(WA_DeleteOnClose, true);
dialog->show();
}


So, make a new class AboutBox, have its ui set up by Ui::AboutBox (look at MainWindow for an example) and create a new one as shown above.

It's dry coded and untested, so there might be mistakes.

JesperWe
16th December 2009, 09:36
Thank you. I got it figured out now. For other Newbie people hitting this thread, here is what I learned:

- Creating a new form in Form Editor only creates the code for the UI object that sets up the form. It doesn't create any actual widget that displays it. This you will have to do yourself.

This is what I ended up with after patching various tips together. Ui::AboutBox is the class generated by Qt Designer.



void MainWindow::on_actionAbout_triggered()
{
QDialog *aboutBox = new QDialog;
Ui::AboutBox ui;
ui.setupUi(aboutBox);
aboutBox->setAttribute( Qt::WA_DeleteOnClose, true );
aboutBox->show();
}