Qt中的QStackedLayout
生活随笔
收集整理的這篇文章主要介紹了
Qt中的QStackedLayout
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 1 Qt中的QStackedLayout
1 Qt中的QStackedLayout
棧式布局管理器(QStackedLayout):
- 所有組件在垂直于屏幕的方向上被管理。
- 每次只有一個組件會顯示在屏幕上。
- 只有最頂層的組件會被最終顯示。
棧式布局管理器的特點:
- 組件大小一致且充滿父組件的顯示區。
- 不能直接嵌套其它布局管理器。
- 能夠自由切換需要顯示的組件。
- 每次能且僅能顯示一個組件。
QStackedLayout的用法概要:
如下為QStackedLayout結合QTimer示例程序:
Widget.h:
Widget.cpp:
#include "Widget.h" #include <QStackedLayout> #include <QHBoxLayout> #include <QtCore> #include <QDebug>Widget::Widget(QWidget *parent) : QWidget(parent),TestBtn1(this), TestBtn2(this), TestBtn3(this), TestBtn4(this) {initControl(); }void Widget::initControl() {QStackedLayout* sLayout = new QStackedLayout();QHBoxLayout* hLayout = new QHBoxLayout();QWidget* widget = new QWidget();QTimer* timer = new QTimer(this);TestBtn1.setText("1st Button");TestBtn2.setText("2rd Button");TestBtn3.setText("3th Button");TestBtn4.setText("Test Button 4: D.T.Software");TestBtn2.setParent(widget);TestBtn3.setParent(widget);hLayout->addWidget(&TestBtn2);hLayout->addWidget(&TestBtn3);widget->setLayout(hLayout);sLayout->addWidget(&TestBtn1); // 0sLayout->addWidget(widget); // 1sLayout->addWidget(&TestBtn4); // 2sLayout->setCurrentIndex(0);setLayout(sLayout);connect(timer, SIGNAL(timeout()), this, SLOT(timerTimeout()));timer->start(2000); }void Widget::timerTimeout() {QStackedLayout* sLayout = dynamic_cast<QStackedLayout*>(layout());if( sLayout != NULL ){int index = (sLayout->currentIndex() + 1) % sLayout->count();sLayout->setCurrentIndex(index);} }Widget::~Widget() {}main.cpp:
#include <QtGui/QApplication> #include "Widget.h"int main(int argc, char *argv[]) {QApplication a(argc, argv);Widget w;w.show();return a.exec(); }參考資料:
總結
以上是生活随笔為你收集整理的Qt中的QStackedLayout的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 点阵的驱动
- 下一篇: 笔记本怎么按热键 笔记本如何使用热键