大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
重要函数:
1.void setCurrentIndex(int); //用下标显示当前页,.从0开始.
2.int count(); //返回页面的数量.
3.void insertWidget(int, QWidget*); //在下标为参数位置插入页.
4.void addWidget(QWidget*); //加上页.
5.void removeWidget(QWidget*); //删除页.
创新互联主营永修网站建设的网络公司,主营网站建设方案,成都app软件开发,永修h5微信平台小程序开发搭建,永修网站营销推广欢迎永修等地区企业咨询
信号:
1.void currentChanged(int); //当前页面发生改变时,发出信号.
2.void widgetRemoved(int); //页面被删除时,发出信号.
首先我们添加一个类,名字自定义,我这里叫MyPicture.不用Qt设计师进行拖拽,而是进行手工布局,因为当部件的量很多或者需要自动生成的时候,手工布局显得更加灵活.
以下是”MyPicture.cpp”下的代码:
#include "MyPicture.h"MyPicture::MyPicture(QWidget *parent) : QWidget(parent) { ui.setupUi(this); //创建一个垂直布局. QVBoxLayout *vertLayout = new QVBoxLayout; for (int i = 0; i < 5; ++i) { //创建一个水平布局. 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#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处,进行拖拽,进行如下布局和命名.
然后是”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() { //先删除系统自动为你添加的两个页面. 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(); //初始化堆积页. 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(); }