系列文章目录
在QT中,如何使用listwidget和stackview实现多窗口切换 ,环境qt5.9;
前言
listwidget, stackview
一、ui设计师设计
二、使用步骤
1.mainwindow.h
代码如下(示例):
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QList>
#include <QWidget>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void addwidget();
private slots:
void on_listWidget_currentRowChanged(int currentRow);
private:
Ui::MainWindow *ui;
QList<QWidget *> m_wdgLst;
};
#endif // MAINWINDOW_H
2.mainwindow.cpp
代码如下(示例):
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "mainform.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
addwidget();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::addwidget()
{
m_wdgLst.clear();
m_wdgLst.push_back(new MainForm);
foreach (QWidget* wdg, m_wdgLst) {
ui->stackedWidget->addWidget(wdg);
}
ui->stackedWidget->setCurrentWidget(m_wdgLst.first());
}
void MainWindow::on_listWidget_currentRowChanged(int currentRow)
{
if (currentRow >= 0 && currentRow < ui->stackedWidget->count())
{
ui->stackedWidget->setCurrentWidget(m_wdgLst.at(currentRow));
}
}