QStackeddialog堆栈窗体应用
QStackeddialog.h QStackeddialog.cpp main.cpp 运行图
QStackeddialog.h
# ifndef QSTACKEDDIALOG_H
# define QSTACKEDDIALOG_H
# include <QDialog>
# include <QListWidget>
# include <QStackedWidget>
# include <QLabel>
class QStackeddialog : public QDialog
{
Q_OBJECT
public :
QStackeddialog ( QWidget * parent = nullptr ) ;
~ QStackeddialog ( ) ;
private :
QListWidget * list;
QStackedWidget * stack;
QLabel * label1;
QLabel * label2;
QLabel * label3;
} ;
# endif
QStackeddialog.cpp
# include "qstackeddialog.h"
# include <QHBoxLayout>
QStackeddialog :: QStackeddialog ( QWidget * parent)
: QDialog ( parent)
{
setWindowTitle ( tr ( "StackedWidget" ) ) ;
list = new QListWidget ( this ) ;
list-> insertItem ( 0 , tr ( "windows1" ) ) ;
list-> insertItem ( 1 , tr ( "windows2" ) ) ;
list-> insertItem ( 2 , tr ( "windows3" ) ) ;
label1 = new QLabel ( tr ( "windwosTest1" ) ) ;
label2 = new QLabel ( tr ( "windowsText2" ) ) ;
label3 = new QLabel ( tr ( "wondowsText3" ) ) ;
stack = new QStackedWidget ( this ) ;
stack-> addWidget ( label1) ;
stack-> addWidget ( label2) ;
stack-> addWidget ( label3) ;
QHBoxLayout * MainLayout = new QHBoxLayout ( this ) ;
MainLayout-> setMargin ( 5 ) ;
MainLayout-> setSpacing ( 5 ) ;
MainLayout-> addWidget ( list) ;
MainLayout-> addWidget ( stack, 0 , Qt:: AlignHCenter) ;
MainLayout-> setStretchFactor ( list, 1 ) ;
MainLayout-> setStretchFactor ( stack, 1 ) ;
connect ( list, SIGNAL ( currentRowChanged ( int ) ) , stack, SLOT ( setCurrentIndex ( int ) ) ) ;
}
QStackeddialog :: ~ QStackeddialog ( ) { }
main.cpp
# include "qstackeddialog.h"
# include <QApplication>
int main ( int argc, char * argv[ ] )
{
QApplication a ( argc, argv) ;
QStackeddialog w;
w. show ( ) ;
return a. exec ( ) ;
}
运行图