溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Qt學習: QStackedWidget和手工代碼布局的程序實例

發(fā)布時間:2020-08-02 07:42:55 來源:網絡 閱讀:1456 作者:閉上左眼 欄目:編程語言

重要函數: 
1.void setCurrentIndex(int); //用下標顯示當前頁,.從0開始. 
2.int count(); //返回頁面的數量. 
3.void insertWidget(int, QWidget*); //在下標為參數位置插入頁. 
4.void addWidget(QWidget*); //加上頁. 
5.void removeWidget(QWidget*); //刪除頁.

信號:

1.void currentChanged(int); //當前頁面發(fā)生改變時,發(fā)出信號. 
2.void widgetRemoved(int); //頁面被刪除時,發(fā)出信號.


首先我們添加一個類,名字自定義,我這里叫MyPicture.不用Qt設計師進行拖拽,而是進行手工布局,因為當部件的量很多或者需要自動生成的時候,手工布局顯得更加靈活.


以下是”MyPicture.cpp”下的代碼:

#include "MyPicture.h"MyPicture::MyPicture(QWidget *parent)
    : QWidget(parent)
{
    ui.setupUi(this);    //創(chuàng)建一個垂直布局.
    QVBoxLayout *vertLayout = new QVBoxLayout;    for (int i = 0; i < 5; ++i)
    {        //創(chuàng)建一個水平布局.
        QHBoxLayout *horiLayout = new QHBoxLayout;        for (int j = 0; j < 5; ++j)
        {
            QLabel *temp = new QLabel("z");
            temp->resize(100,100);            //也添加到vector容器里去,方便調用.
            this->labelArray.push_back(temp);            //把label對象添加到水平布局中去.
            horiLayout->addWidget(temp);
        }        //布局中可以添加布局.
        vertLayout->addLayout(horiLayout);
    }    //最后應用垂直布局.
    this->setLayout(vertLayout);
}

MyPicture::~MyPicture()
{

}void MyPicture::setText(QString str)
{    for (int i = 0; i < 25; ++i)
    {        this->labelArray[i]->setText(str);
    }
}12345678910111213141516171819202122232425262728293031323334353637383940

以下是”MyPicture.h”的代碼:

#ifndef MYPICTURE_H#define MYPICTURE_H#include <QWidget>#include "ui_MyPicture.h"#include <QMessageBox>#include <QLabel>#include <QVBoxLayout>#include <QHBoxLayout>class MyPicture : public QWidget{
    Q_OBJECTpublic:
    MyPicture(QWidget *parent = 0);
    ~MyPicture();    void setText(QString);private:
    Ui::MyPicture ui;
    QVector<QLabel*> labelArray;
};#endif // MYPICTURE_H1234567891011121314151617181920212223242526

然后在c.ui處,進行拖拽,進行如下布局和命名. 
Qt學習: QStackedWidget和手工代碼布局的程序實例


然后是”c.cpp”的代碼:

#include "c.h"c::c(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);
    initStackedWidget();    //連接信號與槽.
    connect(ui.firstButton, SIGNAL(clicked()), this, SLOT(showFirstPageSlot()));
    connect(ui.secondButton, SIGNAL(clicked()), this, SLOT(showSecondPageSlot()));
}

c::~c()
{

}void c::showFirstPageSlot()
{    //設置當前顯示的頁面
    ui.stackedWidget->setCurrentIndex(0);
}void c::showSecondPageSlot()
{
    ui.stackedWidget->setCurrentIndex(1);
}//初始化堆積頁.void c::initStackedWidget()
{    //先刪除系統(tǒng)自動為你添加的兩個頁面.
    ui.stackedWidget->removeWidget(ui.page);
    ui.stackedWidget->removeWidget(ui.page_2);

    MyPicture *temp = new MyPicture;
    temp->setText(QString::fromLocal8Bit("哈哈"));
    ui.stackedWidget->addWidget(temp);

    MyPicture *i = new MyPicture;
    i->setText(QString::fromLocal8Bit("嘻嘻"));
    ui.stackedWidget->addWidget(i);
}123456789101112131415161718192021222324252627282930313233343536373839404142

“c.h”的代碼:

#ifndef C_H#define C_H#include <QtWidgets/QMainWindow>#include "ui_c.h"#include <QStackedWidget>#include <QPushButton>#include <QMessageBox>#include "MyPicture.h"class c : public QMainWindow{
    Q_OBJECTpublic:
    c(QWidget *parent = 0);
    ~c();    //初始化堆積頁.
    void initStackedWidget();private slots:    void showSecondPageSlot();    void showFirstPageSlot();private:
    Ui::cClass ui;
};#endif // C_H12345678910111213141516171819202122232425262728293031

最后是”main.cpp”的代碼:

#include "c.h"#include <QtWidgets/QApplication>#include "MyPicture.h"int main(int argc, char *argv[])
{
    QApplication a(argc, argv);    /*c w;
    w.show();*/

    MyPicture w;
    w.show();    return a.exec();
}


向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI