Qt - proper application code design

It was difficult for me to search for a relevant topic, so here is my question. I started using Qt like two days ago, and therefore I don’t know how to make it work (on the code side).

[offtopic] Here is some story: at first I thought about how to separate my applied logic from its appearance. I had some basic classes, others for the graphical interface (display and control) and some “bridges” between, for example, moving data from class A, in which there were std :: list elements for class B: public QAbstractListView, which is QStringList. But I gave up when I had to use more and more Qt code (HTTP requests, disk I / O, regular expressions). My code began to look like a mess, and I was thinking about reorganizing my code.

(In any case, is it a good idea to combine these two things - the application logic into Qt (sub) classes?) [/ Offtop]

And I came up with another problem, and this, finally, is related to the question in the topic: it is better (say, Qt-way), for example, to have a class with a private member QWebPage and some public methods, slots and signals to work on it, or just add my functionality in a subclass of QWebPage?

+5
source share
1 answer

Inheritance is one of the greatest things about OOP if used correctly.

The “subclass” in all good OO projects must obey a simple rule: IS is a child of the parent parent? This is commonly referred to in the OOP literature as "relationships." And more importantly: the child should always do two things: specialize in generic behavior or expand the functionality of the father. I believe this is the smell of code when a subclass doesn't work either.

, Qt , . .

: QLabel, , , -

class MyScoreBoard : public QLabel
{
private:
   int scoreP1;
   int scoreP2;
   Game *_g;
public:
   MyScoreBoard(QWidget *parent = 0) :
       QLabel(parent)
   {
       scoreP1 = 0;
       scoreP2 = 0;
       connect(_g, SIGNAL(scoreChanged(int,int)), this, SLOT(updateScore(int,int)));
   }
public slot:
    updateScore(int a, int b) {
       scoreP1 = a;
       scoreP2 = b;
       this->setText(QString::number(scoreP1) + "x" + QString::number(scoreP2));
    };

};

, , , , , , ScoreBoard, , HAD , .

: ,

-, , .

+3

All Articles