Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Resolved] Create a base project to show a simple dialog starting from an empty project
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 4.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    realtebo
    wrote on last edited by
    #1

    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.. ?

    1 Reply Last reply
    0
    • R Offline
      R Offline
      realtebo
      wrote on last edited by
      #2

      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"?

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Sanchir Kartiev
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by
          #4

          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.

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          1 Reply Last reply
          0
          • R Offline
            R Offline
            realtebo
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • R Offline
              R Offline
              realtebo
              wrote on last edited by
              #6

              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 .. ?

              1 Reply Last reply
              0
              • JKSHJ Offline
                JKSHJ Offline
                JKSH
                Moderators
                wrote on last edited by
                #7

                [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

                Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                1 Reply Last reply
                0
                • R Offline
                  R Offline
                  realtebo
                  wrote on last edited by
                  #8

                  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

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved