How to pass variable from MainWindow to SecondWindow in Qt C++ - c++

I have a pushbutton on my main form named pushButton_Tar1ex, it has some text on it. I have another pushbutton on my main form named pushButton_RenameTargets.
When I push pushButton_RenameTargets, the following code executes:
void MainWindow::on_pushButton_RenameTargets_clicked()
{
RenameTargets renametargets;
renametargets.target1NameCurrent = ui->pushButton_Tar1ex->text();
renametargets.setModal(true);
renametargets.exec();
}
So my second window is called RenameTargets and the first line of code in the function creates an object renametargets. Then I set renametargets.target1NameCurrent = ui->pushButton_Tar1ex->text();which target1NameCurrent is in the public: portion of renametargets.h. Then in RenameTargets I set the label to the pushbuttontext by:
RenameTargets::RenameTargets(QWidget *parent) :
QDialog(parent),
ui(new Ui::RenameTargets)
{
ui->setupUi(this);
ui->label_currentNameTarget1->setText(target1NameCurrent);
}
Yet nothing appears in the label label_currentNameTarget1.
I have run some qDebug stuff and what happens is when RenameTargets renametargets; is run when I push the button it runs through the RenameTargets ui(new Ui::RenameTargets) part before the renametargets.target1NameCurrent = ui->pushButton_Tar1ex->text(); is declared.
What am I doing wrong? any help would be appreciated.

you set renametargets.target1NameCurrent member variable after constructing your object renametargets... that's the problem, since you use target1NameCurrent in your constructor to set your label.
One solution is to pass target1NameCurrent through the constructor.
void MainWindow::on_pushButton_RenameTargets_clicked()
{
RenameTargets renametargets(this, ui->pushButton_Tar1ex->text());
renametargets.setModal(true);
renametargets.exec();
}
RenameTargets.cpp
RenameTargets::RenameTargets(QWidget *parent, const QString & target1NameCurrent):
QDialog(parent),
ui(new Ui::RenameTargets),
target1NameCurrent(target1NameCurrent)
{
ui->setupUi(this);
ui->label_currentNameTarget1->setText(target1NameCurrent);
}
RenameTargets.h
class RenameTargets{
public:
RenameTargets(QWidget *parent, const QString & target1NameCurrent);
};
Also, you can just initialize label_currentNameTarget1 in another function in RenameTargets after constructing renametargets and initializing renametargets.target1NameCurrent.

Related

Calling a function that is apart of an initialized object after a member variable has changed in another file

Pretty much this window opens up and asks for a bandname. I got it so the characters on the line edit widget get stored in a variable. Problem is I have another file called main window.cpp and I want that variable to to be stored on the list widget on that window. Now I know how to display things on the list widget but I can't figure out a way to get the text after the user finished typing. The bandname var in the main window.cpp file just takes an empty string and I know why but is there any way to trigger the get call after the user has finished typing. Do I have to restrict something in the class like the get function. I've experimented a lot and saw callbacks but I could just use signals and slots. Everything Ive tried just returns an empty string but I need the text after the user has finished typing what he wants. Here is the dialog window named add button
#include "addbutton.h"
#include "ui_addbutton.h"
AddButton::AddButton(QWidget *parent) :
QDialog(parent),
ui(new Ui::AddButton)
{
ui->setupUi(this);
connect(ui->cancel,SIGNAL(released()),this,SLOT(close()));
//Get Text when user presses enter
connect(ui->lineEdit, SIGNAL(editingFinished()),this,SLOT(setBandName()));
}
void AddButton::setBandName(){
bandname = ui->lineEdit->text();
}
void AddButton::updateState(){
pbandname = bandname;
}
QString AddButton::getBandName(){
return bandname;
}
AddButton::~AddButton()
{
delete ui;
}
Here is the main window.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "addbutton.h"
#include "bandinfo.h"
#include "QDebug"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
mediaplayer = new Player;
connect(ui->pushbutton_addBand,SIGNAL(pressed()),this,SLOT(addBand()));
}
void MainWindow::addBand(){
BandInfo band;
AddButton *addband_window = new AddButton;
QString bandname;
addband_window->show();
bandname = addband_window->pbandname;
qDebug() << bandname;
}
MainWindow::~MainWindow()
{
delete ui;
}
I'm not sure I understand your post. Are you saying you want the main window to be notified/updated when the user finishes editing in the "AddButton" class?
If I've got that right it seems pretty straightforward:
Add a signal to the AddButton class. Call it something like "bandNameChanged".
Make the signal pass a string as its argument
Emit the signal from within AddButton::setBandName and pass the string name.
Have the main window connect a slot to the "bandNameChanged" signal when it creates the AddButton.
In the slot, update your list widget

Accesing Base Class Signal using derived Class Object Qt

Hi I am new to the Qt and C++. I have one requirement where I have to access signal of Base class using derived class object. Whether is it possible and if yes then how? I tried to implement it using one simple application, but it is not working.
//Base Class
//Which is emitting signal temp() in it's constructor
class IPCBase : public QWidget
{
Q_OBJECT
public:
explicit IPCBase(QWidget *parent = 0);
~IPCBase();
signals:
void temp();
private:
Ui::IPCBase *ui;
};
//cpp
IPCBase::IPCBase(QWidget *parent) :
QWidget(parent),
ui(new Ui::IPCBase)
{
ui->setupUi(this);
qDebug()<<"coming to base cpp";
emit temp();
}
I have created one derived class named IPCReceiver and using the object of IPCReceiver(derived class) I am trying to use the connect statement in another class(Form) and call the SLOT.It is not giving compilation error but the SLOT is not getting called.
Form::Form(QWidget *parent) :
QWidget(parent),
ui(new Ui::Form)
{
ui->setupUi(this);
IPCReceiver *receiver = new IPCReceiver;
connect(receiver, SIGNAL(temp()), this, SLOT(debug()));
}
Thanks in advance.
Your signal temp() emits in constructor of a class. That's why your connection is unusable. In your logic: at first emits signal in constructor, then you create connection. You must emit signal after creation of the connection.
You generally shouldn't be emitting any signals from constructors of any class deriving from QObject as it makes the class much harder to use. You need to emit the signal from the event loop, after the constructor has finished. There are at least two idioms that express that:
Using a Connection to QObject::destroyed
IPCBase::IPCBase(QWidget *parent) :
QWidget(parent),
ui(new Ui::IPCBase)
{
ui->setupUi(this);
QObject src;
connect(&src, &QObject::destroyed, this, &IPCBase::temp, Qt::QueuedConnection);
}
The src object is used only as a source of a generic signal.
Using QMetaObject::invokeMethod
IPCBase::IPCBase(QWidget *parent) :
QWidget(parent),
ui(new Ui::IPCBase)
{
ui->setupUi(this);
QMetaObject::invokeMethod(this, "temp", Qt::QueuedConnection);
}
As an aside, you don't need to allocate Ui dynamically, you can make it a member variable instead; then the compiler-generated default destructor is sufficient.

How to declare a global variable which calls an object in C++?

mainwindow.cpp
#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::on_pushButton_clicked()
{
audioRecorder = new QAudioRecorder;
QAudioEncoderSettings audioSettings;
audioSettings.setCodec("audio/amr");
audioSettings.setQuality(QMultimedia::HighQuality);
audioRecorder->setEncodingSettings(audioSettings);
audioRecorder->setOutputLocation(QUrl::fromLocalFile("test.amr"));
audioRecorder->record();
}
void MainWindow::on_pushButton_2_clicked()
{
//How to use audioRecorder variable???
}
I want to use the audioRecorder variable into the last method of my code. because when I declare audioRecorder = new QAudioRecorder;, the variable audio recorder is only accessible into the method on_pushButton_clicked(), so I want to make this variable usable into the method n_pushButton_2_clicked(). How to do it ?
Like some person said in the comments, you need to have your QAudioRecorder object declared as a member of your MainWindow class.
So basically you need something like this :
QAudioRecorder* audioRecorder;//This could also be a shared_ptr
Then, your audioRecorder object is created in your on_pushButton_clicked() function.
You can now use it in on_pushButton_2_clicked() function.
However, this is quite unsafe, for example, if you did not create the audioRecorder before calling on_pushButton_2_clicked(), audioRecorder is not going to point to a valid object and this will most certainly crash your project.
So before using it in on_pushButton_2_clicked(), verify if its valid:
if(audioRecorder)
{
// You can use audioRecorder safely here
}

How to add a custom widget to the main window in Qt Creator

I am new to Qt. I took an example from here http://doc.qt.io/qt-5/qtmultimediawidgets-player-example.html.
Now I want to integrate the player in the main window. I created a Qt Widgets application project, I thought, that I would just have to edit the main window code:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
Player* player;
MainWindow::setCentralWidget(player);
}
But it doesn't work and I get the following error:
Starting /home/***/Documents/build-player-Desktop-Debug/player...
The program has unexpectedly finished.
/home/***/Documents/build-player-Desktop-Debug/player crashed
How can I integrate a custom widget which is written in code, without ui in a main window? Thank you in advance.
In your own MainWindow class you can add a widget to the layout of that MainWindow:
MyMainWindow::MyMainWindow(QWidget *parent) :
...
{
this->ui->setupUi(this);
QLabel *myLabel = new QLabel();
this->layout()->addWidget(myLabel);
}
Well, player can't be placed on the window if it is not initialized.
Write something like that :
Player *player = new Player();
I usually add a QWidget (or whatever widget type I'm extending) to my .ui-file in the designer and then promote it to the actual derived type. See the Qt docs for more info on promoting widgets. This means that I can set the base widget's properties and design the window as usual but still get an instance of my special class when the UI is instantiated.
MainWindow:MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
SomeStupidWidget *ssw = new SomeStupidWidget(this); /* important! don't forget about passing "this" as argument, otherwise this could cause a memory leak(Qt handles object's lifetime by means of it's "owner" mechanism)*/
layout()->addWidget(ssw);
}

Add QGraphicsView on form from another class

I have a MainWindow class and Another class. Another class has method createView that create new QGraphicsView. This method I call from MainWindow and I also want to layout this view on my form. It looks like:
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)) {
...
AnotherClass object(this);
object.createView();
...
}
...
void AnotherClass::createView() {
QGraphicsView *gv= new QGraphicsView(mainWindow); // mainWindow - pointer to MainWindow object
gv->show();
}
But it doesn't work so good... actually it does't work at all. And yes, I save pointer on MainWindow object in my Another class as mainWindow, that I take from Another class constructor.
If use
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)) {
...
QGraphicsView *gv= new QGraphicsView(this);
gv->show();
...
}
It will work fine, but this solution doesn't satisfied me.
Problem was in creating variable on stack.