Pass variable from mainwindow to another qt c++ forms - c++

I want to pass three strings (cellTitle,cellPoem,cellGroup) from mainwindows to another dialog. I know when i initiate the second form the values become zeros. I read somewhere it's possible with slots but i don't know how.
mainwindows.h
public:
QString cellTitle,cellPoem,cellGroup;
mainwindows.cpp
void MainWindow::on_tableView_pressed(const QModelIndex &index)
{
cellText = ui->tableView->model()->data(index).toString();
QSqlQuery * qry2 = new QSqlQuery(mydb);
qry2->prepare("select * from Poems where Title='"+cellText+"'");
qry2->exec();
while(qry2->next()){
cellTitle = qry2->value(1).toString();
cellPoem = qry2->value(2).toString();
cellGroup = qry2->value(3).toString();
ui->textEdit->setText(qry2->value(2).toString());
}
}
void MainWindow::on_btnUpdate_clicked()
{
frmUpdate frmupdate;
frmupdate.setModal(true);
frmupdate.exec();
}
frmupdate.cpp
#include "frmupdate.h"
#include "ui_frmupdate.h"
#include <mainwindow.h>
frmUpdate::frmUpdate(QWidget *parent) :
QDialog(parent),
ui(new Ui::frmUpdate)
{
ui->setupUi(this);
MainWindow mainwindow;
ui->lineEdit->setText(mainwindow.cellTitle);
ui->lineEdit_2->setText(mainwindow.cellGroup);
ui->textEdit->setText(mainwindow.cellPoem);
}
frmUpdate::~frmUpdate()
{
delete ui;
}
void frmUpdate::on_btnUpdate_clicked()
{
}
void frmUpdate::on_pushButton_2_clicked()
{
this->close();
}

frmUpdate::frmUpdate(QWidget *parent) :
QDialog(parent),
ui(new Ui::frmUpdate)
{
ui->setupUi(this);
MainWindow mainwindow;
You here have created a new, temporary MainWindow instance only with default constructor being called. Of course, this new instance is then created with empty default texts:
ui->lineEdit->setText(mainwindow.cellTitle);
ui->lineEdit_2->setText(mainwindow.cellGroup);
ui->textEdit->setText(mainwindow.cellPoem);
}
Easiest now would be passing the original main window as parameter:
frmUpdate::frmUpdate(QWidget* parent, MainWindow* mainWindow) :
QDialog(parent),
ui(new Ui::frmUpdate)
{
ui->setupUi(this);
ui->lineEdit->setText(mainwindow->cellTitle);
ui->lineEdit_2->setText(mainwindow->cellGroup);
ui->textEdit->setText(mainwindow->cellPoem);
}
If the parent is the main window, you can simply cast:
frmUpdate::frmUpdate(QWidget* parent) :
QDialog(parent),
ui(new Ui::frmUpdate)
{
ui->setupUi(this);
MainWindow* mainWindow = qobject_cast<MainWindow*>(parent);
if(mainWindow)
{
ui->lineEdit->setText(mainwindow->cellTitle);
ui->lineEdit_2->setText(mainwindow->cellGroup);
ui->textEdit->setText(mainwindow->cellPoem);
}
else
{
// appropriate error handling
}
}
You might search the main window from parent as well:
for(;;)
{
MainWindow* mainWindow = qobject_cast<MainWindow*>(parent);
if(mainWindow)
{
ui->lineEdit->setText(mainwindow->cellTitle);
ui->lineEdit_2->setText(mainwindow->cellGroup);
ui->textEdit->setText(mainwindow->cellPoem);
break;
}
parent = parent->parent();
if(!parent)
{
// appropriate error handling
break;
}
}
All above assuming that MainWindow inherits from QObject...

Related

How to identify the pressed button in Qt C++?

I have 4 buttons on my main window. Each button opens its own window with its own data. How to identify the pressed button to open right window? For example: I press sales button and it opens a window that shows information about ticket sales.
Mainwindow ui
Here is my code from mainwindow h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <sales.h>
#include <theatres.h>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void button_pressed();
private:
Ui::MainWindow *ui;
sales *s;
theatres *t;
};
#endif // MAINWINDOW_H
And here is my code from mainwindow cpp:
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include "build/sqlite/sqlite3.h"
#include <QtSql/QSqlDatabase>
#include <QTableView>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect((*ui).pushButton,SIGNAL(released()), this, SLOT(button_pressed()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::button_pressed()
{
s = new sales(this);
s -> show();
}
As Andy Newman already answered
the shortest solution is a lambda function
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPushButton>
#include <QHBoxLayout>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QHBoxLayout *h_layout = new QHBoxLayout;
centralWidget()->setLayout(h_layout);
for(int c =1; c <= 10; c++)
{
QPushButton *button = new QPushButton(this); // create button
button->setText(QString::number(c)); // set button id
h_layout->addWidget(button); // add a button to the form
// lambda magic
/* connecting a button signal to a lambda function that captures a pointer to a
button and invokes an arbitrary type function. */
connect(button, &QPushButton::clicked, [this, button]() {
pressedButton(button->text());
});
}
}
void MainWindow::pressedButton(const QString &id_button)
{
qDebug("Pressed button: %ls", id_button.utf16());
}
MainWindow::~MainWindow()
{
delete ui;
}
#include "widget.h"
#include "./ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
connect(ui->btn_0,&QPushButton::clicked,this,&Widget::SlotButtonClicked);
connect(ui->btn_1,&QPushButton::clicked,this,&Widget::SlotButtonClicked);
connect(ui->btn_2,&QPushButton::clicked,this,&Widget::SlotButtonClicked);
connect(ui->btn_3,&QPushButton::clicked,this,&Widget::SlotButtonClicked);
}
Widget::~Widget()
{
delete ui;
}
void Widget::SlotButtonClicked()
{
auto sender = this->sender();
if ( sender == ui->btn_0 ) {
// Click btn_0 to open widget0
} else if ( sender == ui->btn_1 ) {
// Click btn_1 to open widget1
} else if ( sender == ui->btn_2 ) {
// Click btn_2 to open widget2
} else if ( sender == ui->btn_3 ) {
// Click btn_3 to open widget3
}
}
If you can use Qt Designer, the best way to do this is to click with button right on the QPushButton (On .ui file in Qt Designer) and click to "Go to Slot", this will create a private slot to this button! In the header file will create the definition, like this:
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
void on_pushButton_3_clicked();
void on_pushButton_4_clicked();
And in the source file (.cpp) will create the "function" clicked pushButton:
void MainWindow::on_pushButton_clicked()
{
}
void MainWindow::on_pushButton_2_clicked()
{
}
void MainWindow::on_pushButton_3_clicked()
{
}
void MainWindow::on_pushButton_4_clicked()
{
}
Inside of the "function" in .cpp, you put the task that you want this button to do, in this case, to open a new window!
When you click "go to slot" in another button, will create another private slot with the respective number (If is the second QPushButton that you create, the private slot will be called by pushButton_2).
The usual way to do this would be to connect the 4 different buttons to 4 different slots. It looks like you are using QtDesigner so that shouldn't be an issue.
If you were generating an array of buttons at run time you'd run into problems and would need a different solution. You could try something like this to pass an array index to the function, for example:
connect(button[x], &QPushButton::clicked, this, [this, x]() { button_pressed(x); });
Or you could do it the Qt way, which would be to call ::setProperty to store data in the button, and then retrieve it from the event, but it's so esoteric that I can't actually remember how to do that...

Passing a variable from MainWindow to new window

In my MainWindow i have a combobox and a select button. When the select button is clicked a new window is opened.
I want to be able to create a QString variable on the MainWindow that contains the text from the combobox, and pass that QString to the new window. The new window is going to perform different tasks, based on the contents of the QString (based on the selection of the combobox).
The following is my code so far...
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "testwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->cmboTestSelect->addItem("{Please Select a Test}");
ui->cmboTestSelect->addItem("Test 1");
ui->cmboTestSelect->addItem("Test 2");
ui->cmboTestSelect->addItem("Test 3");
ui->cmboTestSelect->addItem("Test 4");
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_btnTestSelect_clicked()
{
QString str_TestSelect = ui->cmboTestSelect->currentText(); //stores "Test Name" in string
hide();
Testwindow = new testwindow(this);
Testwindow->show();
}
The simplest way to do this is, as suggested in the comments, to pass the variable as parameter in the constructor of the class Testwindow. Then you can save the value of the string in a private variable in your Testwindow class and do whatever you want with it.
testwindow.h
class Testwindow : public QMainWindow
{
Q_OBJECT
public:
explicit Testwindow(QString text, QWidget *parent = nullptr);
signals:
private:
QString comboBoxText;
};
testwindow.cpp
Testwindow::Testwindow(QString text, QWidget *parent) : QMainWindow(parent)
{
comboBoxText = text; //now you can use comboBoxText in the rest of Testwindow class
}
mainwindow.cpp
void MainWindow::on_btnTestSelect_clicked()
{
QString str_TestSelect = ui->cmboTestSelect->currentText(); //stores "Test Name" in string
hide();
Testwindow *w = new Testwindow(str_TestSelect, this);
w->show();
}

How to get QString from one window to another window, via pressing a button in a 3rd window

New to C++ and Qt as part of a research project (biology) and have been struggling with presumably some quite simple stuff. I'd really appreciate someone's help.
I'm working with a GUI for a pre-existing programme and I'm trying to transfer a QString variable from the QLineEdit of one of the windows (inputform), to the QLineEdit of a second window (output form).
The bit I'm stuck with is that I need the output form to appear, with it's LineEdit pre-populated, when I click a button on a third window (filedialog).
Problem:
At start up --> two windows appear: filedialog and inputform.
User enters data into inputform's QLineEdit
User presses 'transferButton' on filedialog window
On button press --> outputform appears, with a QLineEdit pre-populated with the user's data (from the inputform).
I assume the problem is of the getter/setter variety and my variable is probably going out of scope, but I've tried following lots of similar examples but can't make it work.
Thanks in advance.
Here's my code:
Main.cpp
#include "filedialog.h"
#include "inputform.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
FileDialog w;
InputForm w2;
w.show();
w2.show();
return a.exec();
}
filedialog.h
#ifndef FILEDIALOG_H
#define FILEDIALOG_H
#include <QDialog>
namespace Ui {
class FileDialog;
}
class FileDialog : public QDialog
{
Q_OBJECT
public:
explicit FileDialog(QWidget *parent = nullptr);
~FileDialog();
void setFileName();
QString getFileName();
private slots:
void on_transferButton_clicked();
private:
Ui::FileDialog *ui;
QString fileName;
};
#endif // FILEDIALOG_H
filedialog.ccp
#include "filedialog.h"
#include "ui_filedialog.h"
#include "inputform.h"
#include "ui_inputform.h"
#include "outputform.h"
#include "ui_outputform.h"
FileDialog::FileDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::FileDialog)
{
ui->setupUi(this);
}
FileDialog::~FileDialog()
{
delete ui;
}
void FileDialog::setFileName()
{
InputForm *inputform = new InputForm;
fileName = inputform->ui->inputLineEdit->text();
}
QString FileDialog::getFileName()
{
return fileName;
}
void FileDialog::on_transferButton_clicked()
{
setFileName();
OutPutForm *outputform = new OutPutForm;
outputform->ui->outputLineEdit->setText(getFileName());
outputform->show();
}
inputform.h
#ifndef INPUTFORM_H
#define INPUTFORM_H
#include <QWidget>
namespace Ui {
class InputForm;
}
class InputForm : public QWidget
{
Q_OBJECT
public:
explicit InputForm(QWidget *parent = nullptr);
~InputForm();
Ui::InputForm *ui;
};
#endif // INPUTFORM_H
inputform.ccp
#include "inputform.h"
#include "ui_inputform.h"
InputForm::InputForm(QWidget *parent) :
QWidget(parent),
ui(new Ui::InputForm)
{
ui->setupUi(this);
}
InputForm::~InputForm()
{
delete ui;
}
outputform.h
#ifndef OUTPUTFORM_H
#define OUTPUTFORM_H
#include <QWidget>
namespace Ui {
class OutPutForm;
}
class OutPutForm : public QWidget
{
Q_OBJECT
public:
explicit OutPutForm(QWidget *parent = nullptr);
~OutPutForm();
Ui::OutPutForm *ui;
};
#endif // OUTPUTFORM_H
outputform.ccp
#include "outputform.h"
#include "ui_outputform.h"
OutPutForm::OutPutForm(QWidget *parent) :
QWidget(parent),
ui(new Ui::OutPutForm)
{
ui->setupUi(this);
}
OutPutForm::~OutPutForm()
{
delete ui;
}
Thank you for your brief pointer.
After some playing around:
Setup mainwindow (or in my case main dialog window). Generate inputform instance, connect button to inputform.
FileDialog::FileDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::FileDialog)
{
ui->setupUi(this);
InputForm *inputForm = new InputForm;
connect(ui->transferButton,SIGNAL(clicked()),inputForm,SLOT(getLineEditTextFunc()));
inputForm->show();
}
FileDialog::~FileDialog()
{
delete ui;
}
void FileDialog::on_transferButton_clicked()
{
}
Then from the input form:
Define a function to get the input form's LineEdit text (fileName); and then also generate an output form and populate it's LineEdit with the fileName variable.
InputForm::InputForm(QWidget *parent) :
QWidget(parent),
ui(new Ui::InputForm)
{
ui->setupUi(this);
}
InputForm::~InputForm()
{
delete ui;
}
void InputForm::getLineEditTextFunc()
{
fileName = this->ui->inputLineEdit->text();
OutPutForm *outputform = new OutPutForm;
outputform->ui->outputLineEdit->setText(fileName);
outputform->show();
}

Change parameters of a class when clicking button on different window

I'm pretty new to c++ and Qt but I'm trying to create a simple battleship game. So far I have been able to set up the grid and also place ships but I wan't to put in a menu before the main game window where you choose "difficulty" which will determine how big the grid size for battleship is.
The code for drawing the game board goes as follows:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "boardrender.h"
#include "playerrender.h"
#include "../BattleScrabble/BattleshipBoard.h"
#include "../BattleScrabble/HumanPlayer.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// BattleshipBoard *a = new BattleshipBoard(10);
// Ship *b = new Ship(AircraftCarrier);
ui->battleshipBoardView->setMouseTracking(true);
// BoardRender *scene=new BoardRender(a, ui->battleshipBoardView->width(), this);
// ui->battleshipBoardView->setScene(scene);
ui->battleshipBoardView->resize(ui->battleshipBoardView->width(),ui->battleshipBoardView->width());
Player *b = new HumanPlayer(10,0);
PlayerRender *a = new PlayerRender(b, ui->battleshipBoardView,this);
}
MainWindow::~MainWindow()
{
delete ui;
}
where new HumanPlayer(10,0); is the grid size.
And now the interesting part, the difficulty window:
#include "difficultywindow.h"
#include "ui_difficultywindow.h"
#include "DifficultyWindow.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"
DifficultyWindow::DifficultyWindow(QWidget *parent) :
QDialog(parent),
ui(new Ui::DifficultyWindow)
{
ui->setupUi(this);
}
DifficultyWindow::~DifficultyWindow()
{
delete ui;
}
void DifficultyWindow::on_pushButton_clicked()
{
mWindow = new MainWindow(this);
mWindow->show();
}
when I'm instantiating the "new MainWindow" is there a way to change the HumanPlayer parameters in this class instead?
Any help is greatly appreciated. Thanks :)
edit: and my main.cpp
#include "mainwindow.h"
#include <QApplication>
#include "difficultywindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
DifficultyWindow w;
w.show();
return a.exec();
}
You have to make some modifications, to make your code more understandable you must first add a parameter to MainWidow, in this case it will be called grid
mainwindow.h
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(int grid, QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
Player *b;
PlayerRender *g;
};
mainwindow.cpp
MainWindow::MainWindow(int grid, QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->battleshipBoardView->setMouseTracking(true);
ui->battleshipBoardView->resize(ui->battleshipBoardView->width(),ui->battleshipBoardView->width());
b = new HumanPlayer(grid, 0);
g = new PlayerRender(b, ui->battleshipBoardView,this);
}
Then we will create an enumeration and to handle a signal instead of 3 we will use QButtonGroup:
difficultyWindow.h
class difficultyWindow : public QDialog
{
Q_OBJECT
public:
enum Difficult{
EASY,
MEDIUM,
HARD
};
explicit difficultyWindow(QWidget *parent = 0);
~difficultyWindow();
private slots:
void onClicked(int id);
private:
Ui::difficultyWindow *ui;
MainWindow *mWindow;
QButtonGroup *group;
};
difficultyWindow.h
difficultyWindow::difficultyWindow(QWidget *parent) :
QDialog(parent),
ui(new Ui::difficultyWindow)
{
ui->setupUi(this);
group = new QButtonGroup(this);
group->addButton(ui->easyDiffButton, EASY);
group->addButton(ui->mediumDifficultyButton, MEDIUM);
group->addButton(ui->hardDifficultyButton, HARD);
connect(group, SIGNAL(buttonClicked(int)), this, SLOT(onClicked(int)));
}
difficultyWindow::~difficultyWindow()
{
delete ui;
}
void difficultyWindow::onClicked(int id)
{
int grid;
switch (id) {
case EASY:
grid = 10;
break;
case MEDIUM:
grid = 20;
break;
case HARD:
grid = 30;
break;
}
mWindow = new MainWindow(grid, this);
mWindow->show();
}
Note: eliminate the slots, its are unnecessary for this case.

Signal and slot wrong value sending(Qt c++)

I have written a small program to send data from one form(MainWindow) to another(Dialog) upon a button click. When the button is clicked the value written in the lineEdit of MainWindow is to be displayed on a label in Dialog form!
When I click the button a value is displayed on the label but it is not the same as the value entered in the line edit!
following are the respective codes in the 2 header and 2 cpp files!
MainWindow.h
class MainWindow : public QMainWindow
{
Q_OBJECT
signals:
void sendIntData(int data);
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
}
MainWIndow.cpp
void MainWindow::on_pushButton_clicked()
{
Dialog *dialog1=new Dialog(this);
dialog1->setModal(true);
dialog1->exec();
int o=ui->lineEdit->text().toInt();
connect(this, SIGNAL(sendIntData(int)),dialog1, SLOT(setIntData(int)));
emit sendIntData(o);
}
Dialog.h
class Dialog : public QDialog
{
Q_OBJECT
public slots:
void setIntData(int data);
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
}
Dialog.cpp
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::DIalog)
{
ui->setupUi(this);
QString value=QString::number(index);
ui->label->setText(value);
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::setIntData(int data)
{
index=data;
}
eg-When I click 3 and press the button I get a value 7237481! How can I correct this?
Replace connect and emit in on_pushButton_clicked()
void MainWindow::on_pushButton_clicked()
{
Dialog *dialog1=new Dialog(this);
dialog1->setModal(true);
dialog1->exec();
int o=ui->lineEdit->text().toInt();
connect(this, SIGNAL(sendIntData(int)),dialog1, SLOT(setIntData(int)));
emit sendIntData(o);
}
If only once we convey our dialogue, the importance of signal and slot is not necessary.
It is possible to give this value to the constructor or to do the initialize function and to give it the values.
//way 1:
void MainWindow::on_pushButton_clicked(){
Dialog *dlg = new Dialog();
connect(this, SIGNAL(SendData(int)), dlg, SLOT(slotData(int)));
emit SendData(ui->lineEdit->text().toInt());
dlg->exec();
}
void Dialog::slotData(int arg1)
{
ui->label->setText(QString::number(arg1));
}
//way 2:
void MainWindow::on_pushButton_clicked(){
Dialog* dlg = new Dialog(ui->lineEdit->text().toInt());
dlg->exec();
}
//way 3:
#include "dialog.h"
#include "ui_dialog.h"
#include "QDebug"
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::initialize(int value)
{
ui->label->setText(QString::number(value));
}
void MainWindow::on_pushButton_clicked(){
Dialog *dlg = new Dialog();
dlg->initialize(ui->lineEdit->text().toInt());
dlg->exec();
}
I think you are showing int value which not initialized.
emit signal:
int o=ui->lineEdit->text().toInt();
connect(this, SIGNAL(sendIntData(int)),dialog1, SLOT(setIntData(int)));
emit sendIntData(o);
Show value:
void Dialog::setIntData(int data)
{
ui->label->setText(QString::number(data));
}