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

Writing the Code in MainWindow File

Scheduled Pinned Locked Moved Solved General and Desktop
17 Posts 3 Posters 4.9k 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
    #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