How can QAction ask for input when triggered?

228 views Asked by At

I am trying to have a little OpenGL drawing application which needs files. I need to open a file using the menu bar. What I want is, when a user triggers the action, there is a little popup window that allows the user to enter the input.

Is is possible to do so using Qt? If yes, how?

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

I have tried searching online and similar stackoverflow questions, but to no avail. However, I have seen some applications do this. I found similar tutorials, but they didn't have anything that's like what I want. How can I connect this to the triggering of the action? Also, I am not using Qt Designer.

2

There are 2 answers

0
ΦXocę 웃 Пepeúpa ツ On BEST ANSWER

you are almost there... just add a QfileDialog as suggested in the comments..

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)"));
    });

     ....
}
0
new Q Open Wid On

So I got my own way. I used QInputDialog NOT QFileDialog, as QFileDialog doesn't help and is confusing. I added this to glmainwindow.h:

    void openFileAct()
    {
        QString filePath =  QInputDialog::getText(0, "File path",
                                                  "FIle path", QLineEdit::Normal,
                                                  "");
        openFile(filePath.toStdString());
    }

where openFile is the function that opens the file. I connected the action to openFileAct.