O
O
Oleg Smirnov2016-02-02 11:47:57
Qt
Oleg Smirnov, 2016-02-02 11:47:57

Qt development - structure of a Qt application.?

Where can I find out what the structure of a Qt project looks like? How to separate the user interface from the engine. I would like to know all these subtleties. I am currently developing the STuring program ( GitHub ). There, the interface and the engine are in the same class. I would like to fix this.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
tugo, 2016-02-03
@sovaz1997

Watch this video . This is how, for starters, the structure of your application should look like.
1. Use Qt Designer to build a graphical interface (rather than hand-to-hand placement of widgets, as you do).
2. In the first, most simple application, there should be 4 source code files.
main.cpp
MainWindow.h
MainWindow.cpp
MainWindow.ui

// MainWindow.h
class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget * parent = 0);
    ~MainWindow();

signals:

protected:

private:
    Ui::MainWindow * ui;
};

3. If the number of elements in the MainWindow becomes too large, separate the elements into separate classes, descendants of the QWidget class.
Let's say you have a Tab Widget element in your MainWindow, in which there are 2 tabs.
It is logical to move the logic of the elements in each tab into its own class, i.e. you should have 6 more files in your project:
MyTabWidget1.h
MyTabWidget1.cpp
MyTabWidget1.ui
MyTabWidget2.h
MyTabWidget2.cpp
MyTabWidget2.ui
// MyTabWidget1.h
#pragma once

#include <QWidget>

namespace Ui {
class MyTabWidget1;
}

class MyTabWidget1: public QWidget
{
    Q_OBJECT

public:
    explicit MyTabWidget1(QWidget * parent = 0);
    ~MyTabWidget1();

private:
    Ui::MyTabWidget1* ui;
};

The MainWindow class uses objects of the MyTabWidget1 and MyTabWidget2 classes.
The logic of the MainWindow will dramatically lose weight, everything will be transferred to MyTabWidget1 and MyTabWidget2.
4. Next. Avoid implementing the logic of how a GUI class works. These classes (MyTabWidget1 and MyTabWidget2, MainWindow) should be simple pads, translators of signals from the user into the application logic and visualizers of the application state to the user.
You begin to implement the logic of the application in a separate class.
The exchange of information between Logic <--> MainWindow is assigned to signal slots.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question