Writing the Code in MainWindow File



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



  • Hi ChrisW,

    This link help you.

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



  • @yuvaram

    Thanks, I will have a look at it.


  • Qt Champions 2016

    @ChrisW

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

    You get this structure pr default. :)



  • @mrjj

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



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


  • Qt Champions 2016

    @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



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


  • Qt Champions 2016

    @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);



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


  • Qt Champions 2016

    @ChrisW said:

    ui->setupUi(this);

    move code after this. just for good order.

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


  • Qt Champions 2016

    Hmm
    running ur code i get this

    Which seems fine and as expected?



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


  • Qt Champions 2016

    @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 : )?



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


  • Qt Champions 2016

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



  • @mrjj
    Thanks


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.