QAction 如何在触发时请求输入?
How can QAction ask for input when triggered?
我正在尝试创建一个需要文件的小型 OpenGL 绘图应用程序。我需要使用菜单栏打开一个文件。我想要的是,当用户触发操作时,有一个小弹出窗口 window 允许用户输入。
是否可以使用 Qt 做到这一点?如果是,如何?
glmainwindow.cpp
#include "glmainwindow.h"
#include <QGroupBox>
#include <QMenuBar>
glMainWindow::glMainWindow(fileReader reader, QWidget *parent) : QMainWindow(parent)
{
// initialization(reader);
QGroupBox *box = new QGroupBox(this);
mainLayout = new QGridLayout();
glWidget = new mainWidget(reader.p1, reader.p2);
mainLayout->addWidget(glWidget, 0, 0); //glWindow, 0, 0); //instance, 0, 0); //glWindow, 0, 0); //game, 1, 0); //simpleTex, 0, 0); //cubeTextureWindow, 0, 0);
/* Above FOR simpleGame */
userInput = new QLineEdit;
mainLayout->addWidget(userInput, 1, 0);
box->setLayout(mainLayout);
setCentralWidget(box);
setGeometry(150, 200, 720, 740);
createActions();
createMenus();
}
void glMainWindow::createMenus()
{
glMenuBar = menuBar();
fileMenu = new QMenu("File", this);
fileMenu->addAction(openFileAction);
fileMenu->addAction(closeAction);
glMenuBar->addMenu(fileMenu);
}
void glMainWindow::createActions()
{
openFileAction = new QAction(tr("Open file"), this);
// connect(openFileAction, &QAction::triggered)
closeAction = new QAction("Exit", this);
connect(closeAction, &QAction::triggered, glWidget, &QWidget::close);
}
glMainWindow.h
#ifndef GLMAINWINDOW_H
#define GLMAINWINDOW_H
#include <QPushButton>
#include <QLabel>
#include <QMainWindow>
#include <QGridLayout>
#include <QSlider>
#include <QLineEdit>
#include <QAction>
#include "../roadsFileRead/filereader.h"
#include "mainwidget.h"
class glMainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit glMainWindow(fileReader reader, QWidget *parent = nullptr);
private:
void createMenus();
void createActions();
private:
QGridLayout *mainLayout;
mainWidget *glWidget{nullptr};
QSlider* xSlider;
QSlider* ySlider;
QSlider* zSlider;
QLineEdit *userInput;
QMenuBar *glMenuBar;
QMenu *fileMenu;
QAction *closeAction{nullptr};
QAction *openFileAction{nullptr};
};
#endif // GLMAINWINDOW_H
我尝试在网上搜索类似的Whosebug 问题,但无济于事。但是,我已经看到一些应用程序这样做了。我找到了类似的教程,但他们没有任何我想要的东西。我如何将其与动作的触发联系起来?另外,我没有使用 Qt Designer。
你就快完成了...只需按照评论中的建议添加一个 QfileDialog..
void glMainWindow::createActions()
{
//define the object for the file name:
QString fileName = "";
openFileAction = new QAction(tr("Open file"), this);
connect(openFileAction, &QAction::triggered, []()
{
fileName = QFileDialog::getOpenFileName(this,
tr("Open the file"), "/home/user/path", tr("my Files (*.txt *.csv)"));
});
....
}
所以我有自己的方式。我使用 QInputDialog 而不是 QFileDialog,因为 QFileDialog 没有帮助而且令人困惑。我将此添加到 glmainwindow.h:
void openFileAct()
{
QString filePath = QInputDialog::getText(0, "File path",
"FIle path", QLineEdit::Normal,
"");
openFile(filePath.toStdString());
}
其中 openFile
是打开文件的函数。我将动作连接到 openFileAct。
我正在尝试创建一个需要文件的小型 OpenGL 绘图应用程序。我需要使用菜单栏打开一个文件。我想要的是,当用户触发操作时,有一个小弹出窗口 window 允许用户输入。
是否可以使用 Qt 做到这一点?如果是,如何?
glmainwindow.cpp
#include "glmainwindow.h"
#include <QGroupBox>
#include <QMenuBar>
glMainWindow::glMainWindow(fileReader reader, QWidget *parent) : QMainWindow(parent)
{
// initialization(reader);
QGroupBox *box = new QGroupBox(this);
mainLayout = new QGridLayout();
glWidget = new mainWidget(reader.p1, reader.p2);
mainLayout->addWidget(glWidget, 0, 0); //glWindow, 0, 0); //instance, 0, 0); //glWindow, 0, 0); //game, 1, 0); //simpleTex, 0, 0); //cubeTextureWindow, 0, 0);
/* Above FOR simpleGame */
userInput = new QLineEdit;
mainLayout->addWidget(userInput, 1, 0);
box->setLayout(mainLayout);
setCentralWidget(box);
setGeometry(150, 200, 720, 740);
createActions();
createMenus();
}
void glMainWindow::createMenus()
{
glMenuBar = menuBar();
fileMenu = new QMenu("File", this);
fileMenu->addAction(openFileAction);
fileMenu->addAction(closeAction);
glMenuBar->addMenu(fileMenu);
}
void glMainWindow::createActions()
{
openFileAction = new QAction(tr("Open file"), this);
// connect(openFileAction, &QAction::triggered)
closeAction = new QAction("Exit", this);
connect(closeAction, &QAction::triggered, glWidget, &QWidget::close);
}
glMainWindow.h
#ifndef GLMAINWINDOW_H
#define GLMAINWINDOW_H
#include <QPushButton>
#include <QLabel>
#include <QMainWindow>
#include <QGridLayout>
#include <QSlider>
#include <QLineEdit>
#include <QAction>
#include "../roadsFileRead/filereader.h"
#include "mainwidget.h"
class glMainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit glMainWindow(fileReader reader, QWidget *parent = nullptr);
private:
void createMenus();
void createActions();
private:
QGridLayout *mainLayout;
mainWidget *glWidget{nullptr};
QSlider* xSlider;
QSlider* ySlider;
QSlider* zSlider;
QLineEdit *userInput;
QMenuBar *glMenuBar;
QMenu *fileMenu;
QAction *closeAction{nullptr};
QAction *openFileAction{nullptr};
};
#endif // GLMAINWINDOW_H
我尝试在网上搜索类似的Whosebug 问题,但无济于事。但是,我已经看到一些应用程序这样做了。我找到了类似的教程,但他们没有任何我想要的东西。我如何将其与动作的触发联系起来?另外,我没有使用 Qt Designer。
你就快完成了...只需按照评论中的建议添加一个 QfileDialog..
void glMainWindow::createActions()
{
//define the object for the file name:
QString fileName = "";
openFileAction = new QAction(tr("Open file"), this);
connect(openFileAction, &QAction::triggered, []()
{
fileName = QFileDialog::getOpenFileName(this,
tr("Open the file"), "/home/user/path", tr("my Files (*.txt *.csv)"));
});
....
}
所以我有自己的方式。我使用 QInputDialog 而不是 QFileDialog,因为 QFileDialog 没有帮助而且令人困惑。我将此添加到 glmainwindow.h:
void openFileAct()
{
QString filePath = QInputDialog::getText(0, "File path",
"FIle path", QLineEdit::Normal,
"");
openFile(filePath.toStdString());
}
其中 openFile
是打开文件的函数。我将动作连接到 openFileAct。