Qt (C++) Pass Variable from Dialog to main.cpp - c++

When i start my Program a Dialog-Window pops up and asks me to enter a name. Once i've entered my Name and press the Button it closes the Dialog and opens the main window.
My question is how i get the variable/ Name i just set in the Dialog into another class / my main.cpp
Main.cpp
#include "mainwindow.h"
#include <QApplication>
#include <QtDebug>
#include <QtNetwork>
#include <sstream>
#include "mydialog.h"
using namespace std;
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// Open Dialog
MyDialog mDialog;
mDialog.setModal(true);
mDialog.exec();
//Open Main Window
GW2::MainWindow w;
w.show();
return a.exec();
}
mydialog.cpp
#include "mydialog.h"
#include "ui_mydialog.h"
#include <QDebug>
using namespace std;
MyDialog::MyDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::MyDialog)
{
ui->setupUi(this);
}
MyDialog::~MyDialog()
{
delete ui;
}
void MyDialog::on_pushButton_clicked()
{
QString MYNAME = ui->lineEdit->text();
close();
}
I can get MYNAME here that works after i press the Button but i need to pass the Variable...
mydialog.h
#ifndef MYDIALOG_H
#define MYDIALOG_H
#include <QDialog>
#include <QString>
namespace Ui {
class MyDialog;
}
class MyDialog : public QDialog
{
Q_OBJECT
public:
explicit MyDialog(QWidget *parent = 0);
~MyDialog();
private slots:
void on_pushButton_clicked();
private:
Ui::MyDialog *ui;
};
#endif // MYDIALOG_Hs
I tried using google and search function but didn'T find anything that worked on my project. Hope you can help me. Cheers

Add this in MyDialog:
QString MyDialog::getName()
{
return ui->lineEdit->text();
}
Then do:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// Open Dialog
MyDialog mDialog;
mDialog.setModal(true);
mDialog.exec();
// retrieve the name
QString name = mDialog.getName();
//Open Main Window
GW2::MainWindow w;
w.show();
return a.exec();
}
Note that the dialog could be canceled. You should call accept() rather than close() from on_pushButton_clicked() and later test if the dialog was accepted or not:
if ( mDialog.exec() == QDialog::Accepted )
{
QString name = mDialog.getName();
...

Related

QTabwidget click tab event C++

I am new in QT 4 C++ .I have QT 4 form with QTabwidget like in th picture below.
enter image description here
I want to disply on console the string "aa" by selecting tab on clicking it.
Here is my newForm.h
#ifndef _NEWFORM_H
#define _NEWFORM_H
#include "qwidget.h"
#include "ui_newForm.h"
class newForm : public QDialog {
Q_OBJECT
public:
newForm();
virtual ~newForm();
private:
Ui::newForm widget;
};
#endif /* _NEWFORM_H */
_________________________________________________________________________________________
Here is my main.cpp
#include <QApplication>
#include "newForm.h"
int main(int argc, char *argv[]) {
// initialize resources, if needed
// Q_INIT_RESOURCE(resfile);
QApplication app(argc, argv);
newForm *a = new newForm();
a->show();
// create and show your widgets here
return app.exec();
}
Here is my newForm.cpp
#include "newForm.h"
#include <iostream>
#include <QDebug>
newForm::newForm() {
connect(widget.tabWidget, SIGNAL(stateChanged()), this, SLOT(onTabChanged(int)));
widget.setupUi(this);
}
newForm::~newForm() {
}
void newForm::onTabChanged(int ){
qDebug()<<"aa"<<"\n";
}
On selecting new tab it is not displaying "aa"?How to qDebug() "aa" on console?
First of all, why are you using Qt 4 in 2022?
Next thing use currentIndexChanged(int) istead of stateChanged().
Did you also noticed that stateChanged() doesnt pass an interger which is needed for onTabChanged(int).
You also connected widget.tabWidget which isn't initilized yet.
This code might work:
newForm.h
#ifndef _NEWFORM_H
#define _NEWFORM_H
#include "qwidget.h"
namespace Ui { class newForm; };
class newForm : public QDialog {
Q_OBJECT
public:
newForm();
~newForm();
private:
Ui::newForm *widget;
};
#endif /* _NEWFORM_H */
newForm.cpp
#include "newForm.h"
#include "ui_newForm.h"
#include <QDebug>
newForm::newForm()
: widget(new Ui::newForm)
{
widget->setupUi(this);
connect(widget->tabWidget, SIGNAL(currentChanged(int)), this, SLOT(onTabChanged(int)));
}
void newForm::onTabChanged(int ){
qDebug() << "aa";
}
newForm::~newForm() {
delete widget;
}
main.cpp
#include <QApplication>
#include "newForm.h"
int main(int argc, char *argv[]) {
// initialize resources, if needed
// Q_INIT_RESOURCE(resfile);
QApplication app(argc, argv);
newForm a;
a.show();
// create and show your widgets here
return app.exec();
}

Read the input and print factorial in Qt GUI Application using C++

Using QTCreator, I created the design of a GUI Application. I want to read the input entered by the user from lineEdit and when pushButton is clicked, it should print the factorial of that entered number on the same page. I've read some tutorials but don't understand how to code this using qtc++.
A minimal example is like that:
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QWidget>
#include <QLineEdit>
#include <QPushButton>
#include <QHBoxLayout>
class MainWindow : public QWidget
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void hClicked();
void hTextEdit(const QString& data);
private:
QString m_linedata;
QPushButton button;
QLineEdit lineEdit;
QHBoxLayout layout;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include <iostream>
MainWindow::MainWindow(QWidget *parent)
: QWidget(parent)
{
layout.addWidget(&lineEdit);
layout.addWidget(&button);
this->setLayout(&layout);
connect(&lineEdit, &QLineEdit::textChanged, this, &MainWindow::hTextEdit);
connect(&button, &QPushButton::clicked, this , &MainWindow::hClicked);
}
MainWindow::~MainWindow()
{
}
static unsigned factorial(unsigned n)
{
unsigned result = 1;
for (unsigned i=1; i <= n; i++) {
result *= i;
}
return result;
}
void MainWindow::hClicked()
{
if (m_linedata.size() > 0) {
bool res ;
int toint = m_linedata.toInt(&res);
if (res) {
unsigned fact_result = factorial(toint);
lineEdit.clear();
lineEdit.setText(QString::number(fact_result)); }
}
}
void MainWindow::hTextEdit(const QString &data)
{
m_linedata = data;
}
and main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
Just do anything you like with the data passed to the auxillary buffer.

how to use scene object of main file in other file?

I tried to make tictactoe game using Qt and Qgraphicsview but when I draw x on board using Graphicstextitem in mousePressEvent , X does not appear. how fix that ?
I think the problem is that scene of textitem Different from scene of main file but I do not know how fix that.
main.cpp:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Game *gm = new Game;
Game *rect1=new Game;
gm->scenc->setSceneRect(0,0,800,600);
rect1->setRect(160,100,150,150);
gm->scenc->addItem(rect1);
gm->view->setScene(gm->scenc);
gm->view->show();
return a.exec();
}
in game.cpp:
#include <game.h>
Game::Game()
{
scenc= new QGraphicsScene;
view = new QGraphicsView;
text= new QGraphicsTextItem;
}
void Game::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
if (event->buttons() == Qt::LeftButton )
{
text->setPlainText("X");
text->setFont(QFont("Tahoma",24));
text->setPos((160+160+120)/2,140);
scenc->addItem(text);
}
}
in game.h :
class Game : public QObject , public QGraphicsRectItem
{
Q_OBJECT
public:
Game();
QGraphicsScene *scenc;
QGraphicsView *view;
QGraphicsTextItem *text;
void mousePressEvent(QGraphicsSceneMouseEvent *event);
};
The following example illustrates how to do it the right way. Firstly, you have to notice, that Game::mousePressEvent doesn't override any virtual function. It's a good habit to use the override keyword and to drop the virtual keyword in order to be sure, that a virtual function is overwritten.
Game is not derived from QGraphicsScene and has therefore no mousePressEvent member.
Try the following example app.
MyScene.h
#pragma once
#include <QWidget>
#include <QDebug>
#include <QHBoxLayout>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsTextItem>
#include <QGraphicsSceneMouseEvent>
class MyScene : public QGraphicsScene {
Q_OBJECT
public:
MyScene(QWidget* parent = nullptr) : QGraphicsScene(parent) {
setSceneRect(0, 0, 800, 600);
}
void mouseDoubleClickEvent(QGraphicsSceneMouseEvent* mouseEvent) override {
if (mouseEvent->buttons() == Qt::LeftButton)
{
auto text = new QGraphicsTextItem;
addItem(text);
text->setPlainText("X");
text->setPos(mouseEvent->scenePos());
}
}
private:
QGraphicsView* mView;
QGraphicsTextItem* mText;
};
main.cpp
#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsTextItem>
#include "MyScene.h"
int main(int argc, char* argv[])
{
QApplication a(argc, argv);
auto scene = new MyScene;
auto view = new QGraphicsView;
view->setScene(scene);
view->show();
return a.exec();
}

How to end graphic GUI and do console function(QT)?

I am completely new to QT and I want to prepare one window and take some input from the user then with this input run one console and show output in the console. I have tried to write code after exec but it seems it is not possible:
int main(int argc, char *argv[])
{
int retmain = 0;
QApplication a(argc, argv);
MainWindow w;
w.show();
cout<<"pos500"<<endl;
retmain = a.exec();
cout<<"pos50"<<endl;
//doing something
return retmain;
}
I don't know why but after a.exec(); nothing happens.
So I searched on the internet and found below topic in stackoverflow:
How to call function after window is shown?
But I want to end the graphic window and then do my process.
You need to call QCoreApplication::exit() to make exec return control to you.
After this function has been called, the application leaves the main event loop and returns from the call to exec(). The exec() function returns returnCode. If the event loop is not running, this function does nothing.
A simple example would be:
//mainwindow.h
//////////////////////////////////////////////////
#pragma once
#include <QtWidgets/QMainWindow>
#include <QtCore/QCoreApplication>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
void closeEvent(QCloseEvent *event);
~MainWindow();
};
//mainwindow.cpp
//////////////////////////////////////////////////
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
}
void MainWindow::closeEvent(QCloseEvent *event)
{
QCoreApplication::exit(0);
QMainWindow::closeEvent(event);
}
MainWindow::~MainWindow(){}
//main.cpp
//////////////////////////////////////////////////
#include "mainwindow.h"
#include <QApplication>
#include <iostream>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
a.exec();
std::cout << "test" << std::endl;
return 0;
}

How to import 2 file paths to single application by right click?

Step1: modify the right click menue
Step2: Qt program coding
// main.cpp
int main(int argc, char *argv[])
{
// QSharedMemory shared("Lakers");
// if(shared.attach()) {
// return 0;
// }
// shared.create(1);
QApplication a(argc, argv);
MainWindow w;
w.show();
w.showMainInputParas(argc, argv);
return a.exec();
}
// mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void showMainInputParas(int argc, char *argv[]);
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::showMainInputParas(int argc, char *argv[])
{
for(int i=0; i<argc; i++) {
ui->textEdit->append(QString("%1: %2")
.arg(i)
.arg(QString::fromLocal8Bit(argv[i])));
}
}
Step3: select 2 files and open
Step4: show the 2 file paths in single program(wrong)
I want the 2 paths showed in one single program, and I add QSharememory class in main.cpp, but I still do not know how to pass the 2 file paths into main's argc & argv