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. Adding own UI to dialog

Adding own UI to dialog

Scheduled Pinned Locked Moved Solved General and Desktop
guibuttonclickdialog
12 Posts 2 Posters 13.1k Views
  • 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.
  • A Offline
    A Offline
    aney
    wrote on last edited by
    #1

    Hey,

    I'm a very beginner to Qt. That's my question:

    I created a main window with a button. When I click on the button a new widget appears, but I want to have a dialog. So how do I add an ownmade .ui-file to the
    dialog-class? When I try it the way I did it for the main window I get errors.

    Also I would be happy if you could have a look if I can code something easier or in a better way.

    Thanks a lot for your help!

    main.cpp

    #include "mondev.h"
    #include <QtWidgets/QApplication>
    #include <qt_windows.h>
    
    int main(int argc, char *argv[])
    {
    	QApplication a(argc, argv);
    	MonDev window;
    	window.setWindowFlags(Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint);				
    	window.show();																			
    	return a.exec();
    }
    

    mondev.cpp

    #include "mondev.h"
    #include "ui_mondev.h"
    #include <QtWidgets>
    #include "DialogDreh.h"
    
    MonDev::MonDev(QWidget *parent)
    	: QMainWindow(parent),
    	ui(new Ui::MonDevClass)
    {
    	ui->setupUi(this);
    }
    
    MonDev::~MonDev()
    {
    }
    
    void MonDev::on_menuDreh_clicked()
    {
    	dialogdreh = new DialogDreh(this);
    	dialogdreh->show();
    }
    

    mondev.h

    #ifndef MONDEV_H
    #define MONDEV_H
    
    #include <QtWidgets/QMainWindow>
    #include "ui_mondev.h"
    #include "DialogDreh.h"
    
    namespace Ui {
    	class MonDev;
    }
    
    class MonDev : public QMainWindow
    {
    	Q_OBJECT
    
    	public:
    		MonDev(QWidget *parent = 0);
    		~MonDev();
    
    	private slots:
    		void on_menuDreh_clicked();
    
    	private:
    		Ui::MonDevClass *ui;
    		DialogDreh *dialogdreh;
    };
    #endif // MONDEV_H
    

    DialogDreh.cpp

    #include <QtWidgets>
    #include "DialogDreh.h"
    #include "ui_DialogDreh.h"
    #include <qdialog.h>
    #include <qmainwindow.h>
    
    DialogDreh::DialogDreh(QWidget *parent) 
    	: QDialog(parent)
    {
    }
    
    DialogDreh::~DialogDreh()
    {
    }
    

    DialogDreh.h

    #ifndef DIALOGDREH_H
    #define DIALOGDREH_H
    
    #include <qdialog.h>
    #include "ui_DialogDreh.h"
    #include <qmainwindow.h>
    
    namespace Ui {
    	class DialogDreh;
    }
    
    class DialogDreh : public QDialog
    {
    	Q_OBJECT
    
    	public:
    		DialogDreh(QWidget *parent=0);
    		~DialogDreh();
    };
    
    #endif //DIALOGDREH_H
    
    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      hi , you would create new dialog via
      New ->Qt->Form designer Class->Dialog Without buttons
      then it should have a blank UI file just like mainwindow and you can just add
      widgets to it.

      It seems you almost did that but constructor for you dialog should be like this
      Dialog::Dialog(QWidget *parent) :
      QDialog(parent),
      ui(new Ui::Dialog)
      {
      ui->setupUi(this);
      }

      to use the UI file
      ui(new Ui::Dialog) and ui->setupUi(this);
      so please try via the new menu

      A 1 Reply Last reply
      1
      • mrjjM mrjj

        hi , you would create new dialog via
        New ->Qt->Form designer Class->Dialog Without buttons
        then it should have a blank UI file just like mainwindow and you can just add
        widgets to it.

        It seems you almost did that but constructor for you dialog should be like this
        Dialog::Dialog(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::Dialog)
        {
        ui->setupUi(this);
        }

        to use the UI file
        ui(new Ui::Dialog) and ui->setupUi(this);
        so please try via the new menu

        A Offline
        A Offline
        aney
        wrote on last edited by
        #3

        @mrjj

        Hi mrjj,

        thanks a lot for your answer!
        so I created a new Widget and without the ui(new UI::DialogDreh) and ui->setupUI(this); part everythings ok. When I push the button the window appears.

        But with this part I get an error references to the ui (error C2512: no appropriate default constructor available; and some more).

        What could that be?

        mrjjM 1 Reply Last reply
        0
        • A aney

          @mrjj

          Hi mrjj,

          thanks a lot for your answer!
          so I created a new Widget and without the ui(new UI::DialogDreh) and ui->setupUI(this); part everythings ok. When I push the button the window appears.

          But with this part I get an error references to the ui (error C2512: no appropriate default constructor available; and some more).

          What could that be?

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @aney
          Hi, if you create a new dialog via the New menu, it all should be ok. ?

          that error "no appropriate default constructor available;"
          normally means you create the object with

          A * = new A();

          but A dont have a constructor that takes zero parameters
          A constructor might be
          a(Widget *Parent)
          and wants to be constructed
          A * = new A(this);

          Bot if you copy & paste the error and the line it talks about , it will be easier to guess.

          A 1 Reply Last reply
          0
          • mrjjM mrjj

            @aney
            Hi, if you create a new dialog via the New menu, it all should be ok. ?

            that error "no appropriate default constructor available;"
            normally means you create the object with

            A * = new A();

            but A dont have a constructor that takes zero parameters
            A constructor might be
            a(Widget *Parent)
            and wants to be constructed
            A * = new A(this);

            Bot if you copy & paste the error and the line it talks about , it will be easier to guess.

            A Offline
            A Offline
            aney
            wrote on last edited by
            #5

            @mrjj

            the lines 8 - 13:

            DialogDreh::DialogDreh(QWidget *parent)
            	: QDialog(parent),
            	ui(new Ui::DialogDreh)
            	{
            		ui->setupUi(this);
            	}
            

            the errors:

            1>dialogdreh.cpp(10): error C2512: 'Ui::DialogDreh': no appropriate default constructor available
            1>dialogdreh.cpp(11): error C2614: 'DialogDreh': illegal member initialization: 'ui' is not a base or member
            1>dialogdreh.cpp(12): error C2065: 'ui': undeclared identifier
            1>dialogdreh.cpp(12): error C2227: left of '->setupUi' must point to class/struct/union

            thanks again!

            1 Reply Last reply
            0
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @aney said:
              Hi it seems you just added the lines to your existing dialog ?
              Like the " ui->setupUi(this);"
              you should create DialogDreh via the New ->Qt->Form designer Class->Dialog Without buttons
              so all the needed files are created.
              It seems no UI files has been generated.
              So I guessing you just added the lines ?
              Or?

              A 1 Reply Last reply
              0
              • mrjjM mrjj

                @aney said:
                Hi it seems you just added the lines to your existing dialog ?
                Like the " ui->setupUi(this);"
                you should create DialogDreh via the New ->Qt->Form designer Class->Dialog Without buttons
                so all the needed files are created.
                It seems no UI files has been generated.
                So I guessing you just added the lines ?
                Or?

                A Offline
                A Offline
                aney
                wrote on last edited by
                #7

                @mrjj
                My active window in Qt Designer was my main window (mondev), there I made a new Dialog without Buttons, a new window/file opened, I added a label and saved it in the mondev-folder.

                I'm coding in Visual Studio, so then I had to add the new dialogdreh.ui to my project manually. Also I had to create dialogdreh.cpp and dialogdreh.h manually.
                Is there any way it creates them automatically in my project?

                mrjjM 1 Reply Last reply
                0
                • A aney

                  @mrjj
                  My active window in Qt Designer was my main window (mondev), there I made a new Dialog without Buttons, a new window/file opened, I added a label and saved it in the mondev-folder.

                  I'm coding in Visual Studio, so then I had to add the new dialogdreh.ui to my project manually. Also I had to create dialogdreh.cpp and dialogdreh.h manually.
                  Is there any way it creates them automatically in my project?

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by mrjj
                  #8

                  @aney
                  Ah, if you use Visual Studio, then you must make
                  sure that moc.exe is also run as it creates
                  the needed ui_ file with c++ code.

                  Do you use the qt plugin for Visual Studio ?

                  --create dialogdreh.cpp and dialogdreh.h manually.
                  that sounds a bit wrong as the wizard normally create the files for you.

                  Are you using Express version of studio ?

                  could u try this test project.
                  its default mainwindow that open dialog when u press button.
                  https://www.dropbox.com/s/svg1skt3ikvj0r1/defaultwithdialog.zip?dl=0

                  If it dont work, I guess your are not properly setup to use UI files.

                  A 1 Reply Last reply
                  0
                  • mrjjM mrjj

                    @aney
                    Ah, if you use Visual Studio, then you must make
                    sure that moc.exe is also run as it creates
                    the needed ui_ file with c++ code.

                    Do you use the qt plugin for Visual Studio ?

                    --create dialogdreh.cpp and dialogdreh.h manually.
                    that sounds a bit wrong as the wizard normally create the files for you.

                    Are you using Express version of studio ?

                    could u try this test project.
                    its default mainwindow that open dialog when u press button.
                    https://www.dropbox.com/s/svg1skt3ikvj0r1/defaultwithdialog.zip?dl=0

                    If it dont work, I guess your are not properly setup to use UI files.

                    A Offline
                    A Offline
                    aney
                    wrote on last edited by
                    #9

                    @mrjj

                    Ok, I tried the same code in QtCreator and it worked :D
                    So maybe I just use QtCreator for the beginning... seems to be easier!

                    I used Visual Studio Professional with the Add-In. And yes, know I see under Debug -> moc_mondev.cpp etc. there's a red-minus-sign :/
                    I think I have insufficient knowledge about Qt + VS, maybe just start with QtCreator+Designer :D

                    But thanks so much for your help!

                    mrjjM 1 Reply Last reply
                    0
                    • A aney

                      @mrjj

                      Ok, I tried the same code in QtCreator and it worked :D
                      So maybe I just use QtCreator for the beginning... seems to be easier!

                      I used Visual Studio Professional with the Add-In. And yes, know I see under Debug -> moc_mondev.cpp etc. there's a red-minus-sign :/
                      I think I have insufficient knowledge about Qt + VS, maybe just start with QtCreator+Designer :D

                      But thanks so much for your help!

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @aney
                      Hi, seems something up with the setup. Normally the Add-In does all the work but
                      if using QCreator is fine then its a good solution. :)
                      Its a nice editor with many hidden gems.

                      A 1 Reply Last reply
                      0
                      • mrjjM mrjj

                        @aney
                        Hi, seems something up with the setup. Normally the Add-In does all the work but
                        if using QCreator is fine then its a good solution. :)
                        Its a nice editor with many hidden gems.

                        A Offline
                        A Offline
                        aney
                        wrote on last edited by
                        #11

                        @mrjj
                        Yes, I think I will have a look later, maybe install the Add-In again. But I'm glad, that it wasn't the code - I already doubted me :D
                        Another question: where can I tag this topic as solved?

                        mrjjM 1 Reply Last reply
                        0
                        • A aney

                          @mrjj
                          Yes, I think I will have a look later, maybe install the Add-In again. But I'm glad, that it wasn't the code - I already doubted me :D
                          Another question: where can I tag this topic as solved?

                          mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          @aney
                          Yeah when meta compiler doesn't run, it all get a bit strange. :)

                          In first post , under it. to the right. is Topic Tools button. It can mark as solved.

                          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