QT Creator主窗口-如何从菜单中更改每个元素的界面
QT Creator Main window - how to change the interface for each element from the menu?
我是QT Creator的新手。我确实创建了一个菜单:Login || Open。当登录是点击我想看到一行编辑和按下按钮。当打开被点击,我想在窗口中看到一张图片。我可以根据我在菜单栏上的点击来改变同一窗口的界面吗?我该怎么做呢?
我做了类似的事情——一个有几个主要区域的应用程序,通过顶部的图标栏进行切换。我使用QStackWidget
将不同的应用区域堆叠在一起,我使用设计器创建了一组QAction
,并使用QActionGroup
实现切换。当这些动作被标记为"可检查的"并分组在QActionGroup
中时,QToolBar
只允许一个动作在同一时间处于活动状态。
下面是我的代码的简化摘录:
// MyApp.h
#include <QMainWindow>
class QAction;
class QActionGroup;
namespace Ui {
class MyApp;
}
class MyApp: public QMainWindow
{
Q_OBJECT
public:
explicit MyApp(QWidget *parent = 0);
~MyApp();
public slots:
void showSection(QAction* a);
private:
Ui::MyApp *ui;
QActionGroup* sections;
};
,
//MyApp.cpp
#include "structureapp.h"
#include "ui_structureapp.h"
#include <QActionGroup>
MyApp::MyApp(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MyApp),
sections(new QActionGroup(this)),
{
ui->setupUi(this);
/* Populate section list */
/* Page indices for the stack widget*/
ui->actionSectionOne-> setData(0);
ui->actionSectionTwo-> setData(1);
ui->actionSectionThree-> setData(2);
sections->addAction(ui->actionSectionOne);
sections->addAction(ui->actionSectionTwo);
sections->addAction(ui->actionSectionThree);
ui->mainToolBar->addSeparator();
connect(sections, SIGNAL(triggered(QAction*)), this, SLOT(showSection(QAction*)));
/* Show the default section */
ui->actionContentSection->trigger();
}
MyApp::~MyApp()
{
delete ui;
}
void MyApp::showSection(QAction *a)
{
ui->mainArea->setCurrentIndex(a->data().toInt());
}
可以。正如我前面所解释的,每个菜单项都是一个信号,它连接到一个插槽。对于两个不同的菜单项,你有两个信号,你需要将它们连接到两个不同的插槽。因此,您可以将第一个插槽命名为onLogin
,第二个插槽命名为onOpen
。(选择描述性的名字很有帮助,这样当你周一回来的时候就能理解你的程序了)。
现在,在插槽onLogin
中,您放入用于登录的代码。在插槽onOpen
中,放入其他代码。但是请考虑一下,如果您依次单击两个菜单项会发生什么。这有可能吗?如果不是,您可能需要另一个解决方案。使用QDialog
进行登录是很常见的。当一个对话框处于活动状态时,你不能使用主应用程序的菜单,所以当你忙于登录时,你不能不小心点击onOpen
。
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 使用strcpy将char数组的元素复制到另一个数组
- 使用不带参数的函数访问结构元素
- 给定n个元素的m个集合.在C++中找到出现在最大集合数中的元素
- C++如何通过用户输入删除列表元素
- lower_bound()返回最后一个元素
- 基于多个条件处理地图中的所有元素
- 调整大小后指向元素值的指针unordered_map有效?
- 使用std::transform将一个范围的元素添加到另一个范围中
- 使用函数"remove"删除重复元素
- 具有最大子序列大小的序列,每个元素都相同
- 如何将两个不同矢量的同一位置的两个元素组合在一起
- 如何将元素添加到数组的线程安全函数?
- 有没有办法将谓词中的元素偏移量传递给 std 算法?
- 我想访问std::unique_ptr中的一个特定元素
- 如何通过 getter 函数删除矢量的元素?
- 向量元素的引用地址与它所指向的向量元素的地址不同.为什么
- 从控制台中删除最后打印的元素
- QT Creator主窗口-如何从菜单中更改每个元素的界面