[Resolved] Create a base project to show a simple dialog starting from an empty project



  • Hi !

    I'm studying Qt 5.2. I downloaded the mingw "edition".

    I need you help to have a step-by-step [and updated) info about starting a widget app.

    I do this

    • Start the QtCreator
    • Choose the Other Projects -> Empty Qt Project
    • Choose the source dir

    Actually I got an empty .pro.
    Ok.

    Now I'd like to create just a dialog

    • right-click on project name and add Qt->Qt Desgner Form Class
    • I choose Dialog withouth buttons and leave othe properties untouched
    • Add a button
    • Save all
    • right-click on project and add c++ -> c++ source file, calling it main.cpp

    Now my .pro looks as:

    @
    FORMS +=
    dialog.ui

    HEADERS +=
    dialog.h

    SOURCES +=
    dialog.cpp
    main.cpp
    @

    The main question is: what's the best-practice code ?

    I used this

    @
    #include "dialog.h"
    #include <QApplication>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    Dialog w;
    w.show();

    return a.exec(&#41;;
    

    }
    @

    But i got this error:

    bq. C:\Qt_Projects\Hello10\dialog.h:4: error: QDialog: No such file or directory
    #include <QDialog>
    ^

    What's the problem.. ?



  • Oh ..... I was missing only this:

    @
    QT += widgets
    @

    But ... why Qt Creator can't rebuild the .pro 'automatically' ?

    I found this info googling, but .. how can I understand with module is missing in .pro from error "QDialog is missing"?



  • If this feature will be included in QT Creator, some problems been solved.


  • Moderators

    Hi,

    What you've done is correct. But for convenience, you can select Applications -> Qt Widgets Application to achieve the same result.

    [quote]But … why Qt Creator can’t rebuild the .pro ‘automatically’ ?

    I found this info googling, but .. how can I understand with module is missing in .pro from error “QDialog is missing”?[/quote]Qt Creator can't edit your .pro file automatically because it doesn't know which module QDialog is in, either.

    To find out which module you need, you can search the documentation. I normally use Google, with the help of this "Chrome extension":http://qt-project.org/forums/viewthread/36199/.

    To search offline, you can open Qt Creator and select_Help -> Index_. Then, search for the class you want.

    Example: At the top of the QDialog reference (http://qt-project.org/doc/qt-5/qdialog.html ), it says "qmake: QT += widgets". This tells you what you need to add to your .pro file.



  • Thanks.

    Now I'm going on editing the form.

    I have a label, a lineEdit, two pushButtons, and a h-spacer.

    I'm starting to try to use lineEdit validator.
    I write

    @
    QRegExp regExp("[A-Za-z][1-9][0-9]{0,2}");
    lineEdit->setValidator(new QRegExpValidator(regExp, this));
    @

    But when I compile I got 2 errors

    bq.
    C:\Qt_Projects\Hello10\dialog.cpp:1: In file included from ..\Hello10\dialog.cpp:1:0:
    C:\Qt_Projects\Hello10\dialog.h:15: error: 'QLineEdit' does not name a type
    QLineEdit *lineEdit;
    ^

    and

    bq.
    C:\Qt_Projects\Hello10\dialog.cpp:-1: In constructor 'Dialog::Dialog(QWidget*)':
    C:\Qt_Projects\Hello10\dialog.cpp:11: error: 'lineEdit' was not declared in this scope
    lineEdit->setValidator(new QRegExpValidator(regExp, this));
    ^

    Why? Probably, I think, because header file of the form doesn't declare the label itself.

    So I open header file of the dialog. Actually it look like

    @
    #ifndef DIALOG_H
    #define DIALOG_H

    #include <QDialog>

    namespace Ui {
    class Dialog;
    }

    class Dialog : public QDialog
    {
    Q_OBJECT

    public:
    explicit Dialog(QWidget *parent = 0);
    ~Dialog();

    private:
    Ui::Dialog *ui;
    };

    #endif // DIALOG_H
    @

    So I tried to add into the "section" public this row

    @
    QLabel *label;
    @

    and the include at the top

    @
    #include <QLabel>
    @

    But the second error is still here

    bq. ‘lineEdit’ was not declared in this scope



  • Ok, I found the answer:

    @
    ui->lineEdit ....
    @

    Next step: signals

    @
    connect(ui->cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
    @

    This time every thing works ok, but I cannot understand this why I don't need to declare in .h file .. ?


  • Moderators

    [quote]I cannot understand this why I don't need to declare in .h file .. ?[/quote]Declare what?

    The clicked() signal comes from the QPushButton header. It is is included by "ui_dialog.h", which is auto-generated from dialog.ui when you build your project.

    You will need to declare your reject() slot in your dialog.h, and implement it in dialog.cpp



  • ok, so signals already declared not need to be redeclared... yes, it's obvious now.

    bq. You will need to declare your reject() slot ..

    this is not mine, it's a default slot... like accept()

    See: http://qt-project.org/doc/qt-5/qdialog.html#public-slots

    I'm actually doing experiment extending QDialog, not a main window. So I can use the public slot of QDialog.

    I'm closing thread as resolved


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.