Developing my first hard coded app



  • Hello,

    I have been using the designer for a couple of years but want to hard code the gui myself. I created a new project, which in turn created a main and mainwindow. Is this the right way to go?

    I want to separate the gui classes from main.cpp. Is there a basic example to start from?

    Thanks


  • Qt Champions 2017

    Hi
    I assume hard code is more like hand code?
    Yes, sounds fine with empty mainwindow.

    Just put each gui class in its on .h and .cpp file .

    You will use them in MainWindow any way so no reason to put anything in
    main.cpp.

    Im really not sure why you want to hand code GUI but its good practice anyway.


  • Moderators

    The question "is this the right way to go" is not a good question. The right question would be "is it good for what I need". The main and mainwindow code generated by the wizard is just a template. It doesn't really matter if it's a designer or manual code. The thing that matters is do you need a mainwindow in your app? If you do then by all means that's the right way. If you don't then just delete it and do what you actually need. It's like a 5 seconds operation that you need to do once for your project. Don't get hung on that decision too much. It takes virtually no time to remove or add it back if you need it later.

    If you decide that you want to "convert" the generated designer code to hand coded c++ then it's pretty straight forward. The generated code looks something like this:

    //mainwindow.h
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    private:
        Ui::MainWindow *ui;
    };
    
    //mainwindow.cpp
    MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    

    and the generated header:

    //ui_mainwindow.h
    class Ui_MainWindow
    {
    public:
        QMenuBar *menuBar;
        // other members
    
        void setupUi(QMainWindow *MainWindow) { /* stuff */ }
    };
    

    To change it into hand-crafted code you just simplify the header like so:

    //mainwindow.h
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    public:
        explicit MainWindow(QWidget *parent = 0);
    private:
        QMenuBar *menuBar;
        // other members from the generated class
    };
    

    and move the generated implementation to your constructor:

    //mainwindow.cpp
    MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
    {
        //stuff from the generated setupUi()
    }
    

    There's nothing more to it. If you have a lot of ui code using the designer just takes away the boilerplate code from the c++, and so makes it easier to read, but it doesn't make any major difference



  • The other part that is confusing me is I'm trying to isolate the code that will generate the main window content (the central widget).

    I'll name this class MainWindowContent. Should MainWindowContent subclass QWidget?

    MainWindowContent will use QLayout that will contain other widgets and will have a function that returns the generated gui to MainWindow to be displayed.


  • Qt Champions 2017

    @WhatIf said in Developing my first hard coded app:

    MainWindowContent

    If you want that class to be actually displayed in mainwindow, it would make sense
    to let it be a subclass of QWidget. You could directly use it as centralWidget

    If it can say generate various stuff, it might make sense to let it return a widget that you then
    use/insert to central.

    is there a particular reason you want to code it by hand ?


Log in to reply
 

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