QT multi-form design

The relationship of the form class

In the layout management section, we have said the main interface and the two subsystems are encapsulated into three different classes. First, we should discuss what each class package and secondly to discuss the specific relationship between these three classes.

It can be converted into C ++ .h file identified by the file compiler; Qt Dsigner designed by the well screen will correspond to a file in the project file .ui. Corresponds to a file such as configUI.ui ui_configUI.h file, header file contains the definition of the class Ui :: configUI. This class is fully equivalent to the user interface designed by us, that is to say it includes a definition of the various components of the main interface, and the layout of the interface initialization form setupUi () and the like.

However, it should be noted that this class is just one of our design interface equivalent transformation. We are not included in the actual operation of the class interface (such as the definition of the various grooves and signals). To accomplish specific functions to configure the system, we will create a new class configUI, and let the new class inherits from Ui :: configUI. ConfigUI typically have a class member variable Ui :: configUI class ui, it is possible to obtain various components interface directly ui.

Note that, since the interface class corresponding to the UI configUI are defined in this name space, thus two classes with the same name and the name does not conflict. Relationship between these two classes can be further understood by the following code:

// the class interface definition corresponds configUI Ui namespace; 
namespace {Ui 
    class configUI; 
} 

class configUI: the QWidget public 
{ 
    the Q_OBJECT 

public: 
    Explicit configUI (the QWidget * parent = 0); 
    ~ configUI (); 
// The Ui :: configUI object as a member variable configUI; 
Private: 
    Ui * :: configUI UI; 

Private slots: 
// slot function defined here; 
};

In configUI class you may still need to include member functions, signals and the like, and the slot function. Other interfaces corresponding to the two classes has a similar relationship package, is omitted.

In the layout management, by clicking a button on the main screen can be switched to the appropriate form. Which caused us to think about the relationship between several forms of these classes. Specific approach is that we can target two subsystems of the corresponding class as a member variable of the main interface classes, such as:

{Ui namespace 
    class FrontPage; 
} 

class FrontPage: the QWidget public 
{ 
    the Q_OBJECT 

public: 
    Explicit FrontPage (the QWidget * parent = 0); 
    ~ FrontPage (); 

Private: 
    Ui * :: FrontPage UI; 
    configUI * configWidget; 
    logUI * logWidget; 

Signals: 
    // this definition signal; 

Private slots: 
   // slot function defined here; 
};

So when switching various forms, it can easily be accomplished by the recent show () and hide (). So far, we have completed the design and definition of the class interface, the following have to do is jump to achieve specific work window.

Slot design and function of the signal

Can be learned from the above, we want to achieve that is by clicking on the function of each button to jump to the appropriate window. Therefore, three forms corresponding to the function button corresponding to three slots, the trigger signal that is a function of these grooves clicked (). In three of the declared class frontPage the grooves function as follows:

signals:
    void goToWidget(int);
private slots:
    void on_logSysBtn_clicked();
    void on_frontPageBtn_clicked();
    void on_configSysBtn_clicked();
    void on_quitBtn_clicked();
    void runningWidget(int);
    void on_quitBtn_clicked();

In addition to the three window buttons corresponding slot function, the function also contains other statements, we will explain the wait. In accordance with the past practice, we declare the function button corresponding slot, we should go in order to achieve these functions in order to achieve the jump between forms. However, since this form has the program jumps between certain rules, so the following method:

void frontPage::on_frontPageBtn_clicked()
{
    emit goToWidget(0);
}

void frontPage::on_configSysBtn_clicked()
{
    emit goToWidget(1);
}

void frontPage::on_logSysBtn_clicked()
{
    emit goToWidget(2);
}

That is, each retransmission function button corresponding groove goToWidget (int) signal, transmitting different buttons different integers. Here we use the keyword display emit a transmission signal, which is usually related parties, and we use the hair in different Qt Designer, but is essentially the same. Since this is our own definition signal, and the signal itself is a function, it is necessary to declare this signal frontPage class, particularly see above example code.

We (int) signals and slots associated with this function runningWidget. That click the button corresponding to the different forms will be executed runningWidget (int) slot function. But will be executed according to different blocks shaping parameters passed inside the function. Sample Code runningWidget function is as follows:

void frontPage::runningWidget(int widgetNum)
{
    switch (widgetNum) {
    case 0:
        ui->introTextBrowser->show();
        configWidget->hide();
        logWidget->hide();
        break;
    case 1:
        ui->introTextBrowser->hide();
        configWidget->show();
        logWidget->hide();
        break;
    case 2:
        ui->introTextBrowser->hide();
        configWidget->hide();
        logWidget->show();
        break;
    }
}

As can be seen from the above code, by passing shaping different parameters can be displayed in one of three forms, and at the same time hiding the other two forms. By the above method, so that you can switch between the multi-form. In addition, the basic method of realization of the slot function exit button is the direct exit is called.

发布了294 篇原创文章 · 获赞 381 · 访问量 142万+

Guess you like

Origin blog.csdn.net/sinat_20265495/article/details/104276926