Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Forum Updated on Feb 6th

    Solved Writing the Code in MainWindow File

    General and Desktop
    3
    17
    3835
    Loading More Posts
    • 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
      ChrisW last edited by

      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 Reply Quote 0
      • yuvaram
        yuvaram last edited by

        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 Reply Quote 2
        • C
          ChrisW @yuvaram last edited by

          @yuvaram

          Thanks, I will have a look at it.

          mrjj 1 Reply Last reply Reply Quote 0
          • mrjj
            mrjj Lifetime Qt Champion @ChrisW last edited by

            @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 Reply Quote 2
            • C
              ChrisW @mrjj last edited by

              @mrjj

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

              1 Reply Last reply Reply Quote 0
              • C
                ChrisW @mrjj last edited by

                @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!

                mrjj 1 Reply Last reply Reply Quote 0
                • mrjj
                  mrjj Lifetime Qt Champion @ChrisW last edited by mrjj

                  @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 Reply Quote 1
                  • C
                    ChrisW last edited by

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

                    mrjj 1 Reply Last reply Reply Quote 0
                    • mrjj
                      mrjj Lifetime Qt Champion @ChrisW last edited by 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 1 Reply Last reply Reply Quote 2
                      • C
                        ChrisW @mrjj last edited by

                        @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 Reply Quote 0
                        • mrjj
                          mrjj Lifetime Qt Champion last edited by

                          @ChrisW said:

                          ui->setupUi(this);

                          move code after this. just for good order.

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

                          mrjj 1 Reply Last reply Reply Quote 3
                          • mrjj
                            mrjj Lifetime Qt Champion @mrjj last edited by mrjj

                            Hmm
                            running ur code i get this

                            Which seems fine and as expected?

                            C 1 Reply Last reply Reply Quote 3
                            • C
                              ChrisW @mrjj last edited by 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.

                              mrjj 1 Reply Last reply Reply Quote 0
                              • mrjj
                                mrjj Lifetime Qt Champion @ChrisW last edited by

                                @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 Reply Quote 3
                                • C
                                  ChrisW @mrjj last edited by

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

                                  mrjj 1 Reply Last reply Reply Quote 0
                                  • mrjj
                                    mrjj Lifetime Qt Champion @ChrisW last edited by

                                    @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 Reply Quote 3
                                    • C
                                      ChrisW @mrjj last edited by

                                      @mrjj
                                      Thanks

                                      1 Reply Last reply Reply Quote 0
                                      • First post
                                        Last post