QT Creator主窗口-如何从菜单中更改每个元素的界面

QT Creator Main window - how to change the interface for each element from the menu?

本文关键字:元素 界面 菜单 窗口 Creator QT      更新时间:2023-10-16

我是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