Qt学习: QStackedWidget和手工代码布局的程序实例
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Qt学习: QStackedWidget和手工代码布局的程序实例相关的知识,希望对你有一定的参考价值。
重要函数:
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); //当前页面发生改变时,发出信号.
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 <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处,进行拖拽,进行如下布局和命名.
然后是”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 <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(); }
本文出自 “13109855” 博客,请务必保留此出处http://13119855.blog.51cto.com/13109855/1973111
以上是关于Qt学习: QStackedWidget和手工代码布局的程序实例的主要内容,如果未能解决你的问题,请参考以下文章