重慶分公司,新征程啟航
為企業(yè)提供網(wǎng)站建設(shè)、域名注冊(cè)、服務(wù)器等服務(wù)
為企業(yè)提供網(wǎng)站建設(shè)、域名注冊(cè)、服務(wù)器等服務(wù)
重要函數(shù):
1.void setCurrentIndex(int); //用下標(biāo)顯示當(dāng)前頁(yè),.從0開始.
2.int count(); //返回頁(yè)面的數(shù)量.
3.void insertWidget(int, QWidget*); //在下標(biāo)為參數(shù)位置插入頁(yè).
4.void addWidget(QWidget*); //加上頁(yè).
5.void removeWidget(QWidget*); //刪除頁(yè).
創(chuàng)新互聯(lián)主營(yíng)永修網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,成都app軟件開發(fā),永修h5微信平臺(tái)小程序開發(fā)搭建,永修網(wǎng)站營(yíng)銷推廣歡迎永修等地區(qū)企業(yè)咨詢
信號(hào):
1.void currentChanged(int); //當(dāng)前頁(yè)面發(fā)生改變時(shí),發(fā)出信號(hào).
2.void widgetRemoved(int); //頁(yè)面被刪除時(shí),發(fā)出信號(hào).
首先我們添加一個(gè)類,名字自定義,我這里叫MyPicture.不用Qt設(shè)計(jì)師進(jìn)行拖拽,而是進(jìn)行手工布局,因?yàn)楫?dāng)部件的量很多或者需要自動(dòng)生成的時(shí)候,手工布局顯得更加靈活.
以下是”MyPicture.cpp”下的代碼:
#include "MyPicture.h"MyPicture::MyPicture(QWidget *parent) : QWidget(parent) { ui.setupUi(this); //創(chuàng)建一個(gè)垂直布局. QVBoxLayout *vertLayout = new QVBoxLayout; for (int i = 0; i < 5; ++i) { //創(chuàng)建一個(gè)水平布局. QHBoxLayout *horiLayout = new QHBoxLayout; for (int j = 0; j < 5; ++j) { QLabel *temp = new QLabel("z"); temp->resize(100,100); //也添加到vector容器里去,方便調(diào)用. this->labelArray.push_back(temp); //把label對(duì)象添加到水平布局中去. horiLayout->addWidget(temp); } //布局中可以添加布局. vertLayout->addLayout(horiLayout); } //最后應(yīng)用垂直布局. 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#include "ui_MyPicture.h"#include #include #include #include class MyPicture : public QWidget{ Q_OBJECTpublic: MyPicture(QWidget *parent = 0); ~MyPicture(); void setText(QString);private: Ui::MyPicture ui; QVector labelArray; };#endif // MYPICTURE_H1234567891011121314151617181920212223242526
然后在c.ui處,進(jìn)行拖拽,進(jìn)行如下布局和命名. setCurrentIndex(0);
}void c::showSecondPageSlot()
{
ui.stackedWidget->setCurrentIndex(1);
}//初始化堆積頁(yè).void c::initStackedWidget()
{ //先刪除系統(tǒng)自動(dòng)為你添加的兩個(gè)頁(yè)面.
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#include "ui_c.h"#include #include #include #include "MyPicture.h"class c : public QMainWindow{ Q_OBJECTpublic: c(QWidget *parent = 0); ~c(); //初始化堆積頁(yè). void initStackedWidget();private slots: void showSecondPageSlot(); void showFirstPageSlot();private: Ui::cClass ui; };#endif // C_H12345678910111213141516171819202122232425262728293031
最后是”main.cpp”的代碼:
#include "c.h"#include#include "MyPicture.h"int main(int argc, char *argv[]) { QApplication a(argc, argv); /*c w; w.show();*/ MyPicture w; w.show(); return a.exec(); }