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. Writing the Code in MainWindow File
Forum Updated to NodeBB v4.3 + New Features

Writing the Code in MainWindow File

Scheduled Pinned Locked Moved Solved General and Desktop
17 Posts 3 Posters 4.7k Views 2 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.
  • C Offline
    C Offline
    ChrisW
    wrote on last edited by
    #1

    Hi programmers,

    I was looking at the style that the examples use to create applications. I noticed that they write all the code in the file that is usually called mainwindow.cpp (this file name can be anything, but mainwindow is most common) and in the file that is usually called main.cpp, they just call for the window to be opened. Can you tell me how to implement that or redirect me to a forum post that shows how to do that? This seems like it would be a common question.

    Thanks.

    1 Reply Last reply
    0
    • yuvaramY Offline
      yuvaramY Offline
      yuvaram
      wrote on last edited by
      #2

      Hi ChrisW,

      This link help you.

      http://doc.qt.io/qt-5/examples-mainwindow.html

      Yuvaram Aligeti
      Embedded Qt Developer
      : )

      C 1 Reply Last reply
      2
      • yuvaramY yuvaram

        Hi ChrisW,

        This link help you.

        http://doc.qt.io/qt-5/examples-mainwindow.html

        C Offline
        C Offline
        ChrisW
        wrote on last edited by
        #3

        @yuvaram

        Thanks, I will have a look at it.

        mrjjM 1 Reply Last reply
        0
        • C ChrisW

          @yuvaram

          Thanks, I will have a look at it.

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

          @ChrisW

          Hi
          If you create a new Qt widget application via
          File->New->Application-> Qt widget application

          You get this structure pr default. :)

          C 2 Replies Last reply
          2
          • mrjjM mrjj

            @ChrisW

            Hi
            If you create a new Qt widget application via
            File->New->Application-> Qt widget application

            You get this structure pr default. :)

            C Offline
            C Offline
            ChrisW
            wrote on last edited by
            #5

            @mrjj

            Thanks so much. I will try this tomorrow when I wake up.

            1 Reply Last reply
            0
            • mrjjM mrjj

              @ChrisW

              Hi
              If you create a new Qt widget application via
              File->New->Application-> Qt widget application

              You get this structure pr default. :)

              C Offline
              C Offline
              ChrisW
              wrote on last edited by
              #6

              @mrjj

              Hi, so I went to Qt Creator and used the Qt widget application and I was unable to get it to work. I put my code in the mainwindow constructor, right before ui->setupUi(layout);

              MainWindow::MainWindow(QWidget *parent) :
                  QMainWindow(parent),
                  ui(new Ui::MainWindow)
              {
                  QWidget *window = new QWidget();
                  window->setWindowTitle("My App");
              
                  QGridLayout *layout = new QGridLayout;
                  QLabel *label1 = new QLabel("Name: ");
                  QLineEdit *textName = new QLineEdit;
              
                  layout->addWidget(label1, 0, 0);
                  layout->addWidget(textName, 0, 1);
              
                  window->setLayout(layout);
                  ui->setupUi(this);
              }
              

              So a window shows up, but without the widgets. Thanks!

              mrjjM 1 Reply Last reply
              0
              • C ChrisW

                @mrjj

                Hi, so I went to Qt Creator and used the Qt widget application and I was unable to get it to work. I put my code in the mainwindow constructor, right before ui->setupUi(layout);

                MainWindow::MainWindow(QWidget *parent) :
                    QMainWindow(parent),
                    ui(new Ui::MainWindow)
                {
                    QWidget *window = new QWidget();
                    window->setWindowTitle("My App");
                
                    QGridLayout *layout = new QGridLayout;
                    QLabel *label1 = new QLabel("Name: ");
                    QLineEdit *textName = new QLineEdit;
                
                    layout->addWidget(label1, 0, 0);
                    layout->addWidget(textName, 0, 1);
                
                    window->setLayout(layout);
                    ui->setupUi(this);
                }
                

                So a window shows up, but without the widgets. Thanks!

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

                @ChrisW said:
                Hi
                You dont set parent
                QWidget *window = new QWidget( this );
                and you also might need window->show()
                also mainwindow is special so u also need
                setCentral(window) to add into center area of mainwindow.

                When dont u use the visual editor to insert widgets?
                much easier than code.
                http://doc.qt.io/qt-5/designer-widget-mode.html

                1 Reply Last reply
                1
                • C Offline
                  C Offline
                  ChrisW
                  wrote on last edited by
                  #8

                  @mrjj

                  Hi,

                  So I did this:

                  QWidget *window = new QWidget(this);

                  and the window->show() is located in main.cpp file. Also, setCentral is unrecognizable in the compiler. I couldn't find anything in Qt reference to speak about it; however, I saw it used in a sample tutorial program. I thought that it was a user defined function. And for the GUI builder, I'm more of a hobbyist than a programmer that's out making dozens of programs a month, trying to do it in the most efficient way possible :). So, I take joy in writing code.

                  The good news is that I'm seeing the widgets in the mainwindow after using this:

                  QWidget *window = new QWidget(this);

                  But, it is on the top left and is very, very small. Something looks off.

                  mrjjM 1 Reply Last reply
                  0
                  • C ChrisW

                    @mrjj

                    Hi,

                    So I did this:

                    QWidget *window = new QWidget(this);

                    and the window->show() is located in main.cpp file. Also, setCentral is unrecognizable in the compiler. I couldn't find anything in Qt reference to speak about it; however, I saw it used in a sample tutorial program. I thought that it was a user defined function. And for the GUI builder, I'm more of a hobbyist than a programmer that's out making dozens of programs a month, trying to do it in the most efficient way possible :). So, I take joy in writing code.

                    The good news is that I'm seeing the widgets in the mainwindow after using this:

                    QWidget *window = new QWidget(this);

                    But, it is on the top left and is very, very small. Something looks off.

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

                    @ChrisW
                    Hi
                    sorry its
                    http://doc.qt.io/qt-5/qmainwindow.html#setCentralWidget
                    places in the center

                    But, it is on the top left and is very, very small.
                    You might need to scale it
                    window->resize(400,400);

                    C 1 Reply Last reply
                    2
                    • mrjjM mrjj

                      @ChrisW
                      Hi
                      sorry its
                      http://doc.qt.io/qt-5/qmainwindow.html#setCentralWidget
                      places in the center

                      But, it is on the top left and is very, very small.
                      You might need to scale it
                      window->resize(400,400);

                      C Offline
                      C Offline
                      ChrisW
                      wrote on last edited by
                      #10

                      @mrjj

                      Haha! Yeah, that was the one that I was looking at when you spoke of setCentral(). However, when I use setCentralWidget(), the widgets are completely gone from the program. To make it easier, I attached three files below to make the communication easier:

                      mainwindow file

                      #include "mainwindow.h"
                      #include "ui_mainwindow.h"
                      #include <QApplication>
                      #include <QtGui>
                      #include <QtCore>
                      #include <QGridLayout>
                      #include <QLabel>
                      #include <QLIneEdit>
                      #include <QMainWindow>
                      
                      
                      MainWindow::MainWindow(QWidget *parent) :
                          QMainWindow(parent),
                          ui(new Ui::MainWindow)
                      {
                          QWidget *window = new QWidget(this);
                          window->setWindowTitle("My App");
                      
                          QGridLayout *layout = new QGridLayout;
                          QLabel *label1 = new QLabel("Name: ");
                          QLineEdit *textName = new QLineEdit;
                      
                          layout->addWidget(label1, 0, 0);
                          layout->addWidget(textName, 0, 1);
                      
                          window->setLayout(layout);
                          setCentralWidget(window);
                          ui->setupUi(this);
                      }
                      
                      MainWindow::~MainWindow()
                      {
                          delete ui;
                      }
                      
                      

                      main.cpp file

                      #include "mainwindow.h"
                      #include <QApplication>
                      
                      int main(int argc, char *argv[])
                      {
                          QApplication a(argc, argv);
                          MainWindow w;
                          w.show();
                      
                          return a.exec();
                      }
                      
                      

                      header file

                      #ifndef MAINWINDOW_H
                      #define MAINWINDOW_H
                      
                      #include <QMainWindow>
                      
                      namespace Ui {
                      class MainWindow;
                      }
                      
                      class MainWindow : public QMainWindow
                      {
                          Q_OBJECT
                      
                      public:
                          explicit MainWindow(QWidget *parent = 0);
                          ~MainWindow();
                      
                      private:
                          Ui::MainWindow *ui;
                      };
                      
                      #endif // MAINWINDOW_H
                      

                      Hopefully, it makes it easier. I haven't written a lot of code yet. I'm just trying to get to functionality to work first. Thanks again.

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

                        @ChrisW said:

                        ui->setupUi(this);

                        move code after this. just for good order.

                        Try setting size of window
                        window->resize(400,400);

                        mrjjM 1 Reply Last reply
                        3
                        • mrjjM mrjj

                          @ChrisW said:

                          ui->setupUi(this);

                          move code after this. just for good order.

                          Try setting size of window
                          window->resize(400,400);

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

                          Hmm
                          running ur code i get this

                          Which seems fine and as expected?

                          C 1 Reply Last reply
                          3
                          • mrjjM mrjj

                            Hmm
                            running ur code i get this

                            Which seems fine and as expected?

                            C Offline
                            C Offline
                            ChrisW
                            wrote on last edited by ChrisW
                            #13

                            @mrjj

                            Genius!! You beat me to it. Yeah, it is running now. The main window is larger than I wanted, but I'm sure I should be able to figure that out. I'm about to mark this as solved. I noticed that the syntax is a lot more advanced than the C++ that I learned in college or at a level beyond any textbook that I was able to find on amazon. I'm talking mostly with passing parameters into functions. I actually saw a constructor being passed into a function once. I also do unreal engine programming, which has functions and complex parameters being passed into them. Any text that you can think of for this high level of C++? Thanks again.

                            mrjjM 1 Reply Last reply
                            0
                            • C ChrisW

                              @mrjj

                              Genius!! You beat me to it. Yeah, it is running now. The main window is larger than I wanted, but I'm sure I should be able to figure that out. I'm about to mark this as solved. I noticed that the syntax is a lot more advanced than the C++ that I learned in college or at a level beyond any textbook that I was able to find on amazon. I'm talking mostly with passing parameters into functions. I actually saw a constructor being passed into a function once. I also do unreal engine programming, which has functions and complex parameters being passed into them. Any text that you can think of for this high level of C++? Thanks again.

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

                              @ChrisW
                              Hu
                              Super :)
                              You can control the initial size of mainwindow directly in the UI file. simply resize it.
                              You also call setGeometry

                              Can you give example of such "passing parameters" as im not 100% sure what you mean : )?

                              C 1 Reply Last reply
                              3
                              • mrjjM mrjj

                                @ChrisW
                                Hu
                                Super :)
                                You can control the initial size of mainwindow directly in the UI file. simply resize it.
                                You also call setGeometry

                                Can you give example of such "passing parameters" as im not 100% sure what you mean : )?

                                C Offline
                                C Offline
                                ChrisW
                                wrote on last edited by
                                #15

                                @mrjj

                                I can't remember what function that was. It was about a year ago when I saw. But I'm about to give you an example.

                                John(const Lisa &key)

                                and to use it,

                                John(Lisa(3, 4, 2));

                                I wouldn't think to do this. I would actually try to create a constant Lisa object that is passed by reference and assign her a value and past it into the function, which wouldn't compile though. I will keep an eye open with an actual Qt example. Thanks again.

                                mrjjM 1 Reply Last reply
                                0
                                • C ChrisW

                                  @mrjj

                                  I can't remember what function that was. It was about a year ago when I saw. But I'm about to give you an example.

                                  John(const Lisa &key)

                                  and to use it,

                                  John(Lisa(3, 4, 2));

                                  I wouldn't think to do this. I would actually try to create a constant Lisa object that is passed by reference and assign her a value and past it into the function, which wouldn't compile though. I will keep an eye open with an actual Qt example. Thanks again.

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

                                  @ChrisW said:

                                  John(Lisa(3, 4, 2));

                                  Its look like Lisa object being directly created and send to function.
                                  Normally as you say
                                  Lisa mylisa(xx);
                                  john(mylisa);
                                  Guess its due to const.
                                  Not sure what book will cover such things. its just plain c++;
                                  so most would cover.

                                  C 1 Reply Last reply
                                  3
                                  • mrjjM mrjj

                                    @ChrisW said:

                                    John(Lisa(3, 4, 2));

                                    Its look like Lisa object being directly created and send to function.
                                    Normally as you say
                                    Lisa mylisa(xx);
                                    john(mylisa);
                                    Guess its due to const.
                                    Not sure what book will cover such things. its just plain c++;
                                    so most would cover.

                                    C Offline
                                    C Offline
                                    ChrisW
                                    wrote on last edited by
                                    #17

                                    @mrjj
                                    Thanks

                                    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