QT Not Updating Second UI - c++

I have 2 UIs and I am trying to update one from the other. I have the classes phone, user_ui, test_ui, and user_transmitter. The user_ui is the one that a user can interact with such as if the user pressed the toggle button on the UI of the user_ui then the status should be displayed on the UI of the test_ui. The user_transmitter transmits the inputs and outputs and that's its only job, there is no UI to it. The phone class just contains the variables and nothing else.
user_ui.h:
#ifndef USER_UI_H
#define USER_UI_H
#include <QMainWindow>
#include <QTimer>
#include <phone.h>
#include "user_transmitter.h"
#include "test_ui.h"
using namespace std;
QT_BEGIN_NAMESPACE
namespace Ui { class user_ui; }
QT_END_NAMESPACE
class user_ui : public QMainWindow
{
Q_OBJECT
public:
user_ui(QWidget *parent = nullptr);
~user_ui();
Phone phone;
QTimer *timer;
private:
Ui::user_ui *ui;
user_transmitter user_t;
void updateUI();
void connections();
signals:
void toggeButton(int);
private slots:
void toggleButton1();
void update();
};
#endif // USER_UI_H
user_ui.cpp:
#include "user_ui.h"
#include "ui_user_ui.h"
#include <QMessageBox>
user_ui::user_ui(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::user_ui)
{
ui->setupUi(this);
update();
connections();
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
user_ui::~user_ui()
{
delete ui;
}
void user_ui::update()
{
ui->status->setText((phone.status) ? "On" : "Off");
}
void user_ui::connections() {
QObject::connect(this, &user_ui::toggleButton, &user_t, &user_transmitter::toggleButton);
connect(ui->button, SIGNAL(released()), this, SLOT(toggleButton1()));
}
void user_ui::toggleButton1() {
phone.status= !phone.status;
emit toggleButton(1);
}
user_transmitter.h:
#ifndef USER_TRANSMITTER_H
#define USER_TRANSMITTER_H
#include <vector>
#include "phone.h"
#include "test_ui.h"
class user_transmitter : public QObject
{
Q_OBJECT
public:
explicit user_transmitter(QObject *parent = nullptr);
Phone phones;
public slots:
void toggleButton(int);
private:
void setUpSignals();
test_ui test;
signals:
void button(int,bool);
};
#endif // USER_TRANSMITTER_H
user_transmitter.cpp:
#include "user_transmitter.h"
user_transmitter::user_transmitter(QObject *parent) : QObject(parent)
{
setUpSignals();
}
void user_transmitter::setUpSignals(){
QObject::connect(this, &user_transmitter::button, &test, &test_ui::button);
}
void user_transmitter::button(int index)
{
phones.status = !phones.status;
cout << "From transmitter: " << phones.status<< endl;
emit button(index, phones.status);
}
test_ui.h:
#ifndef TEST_UI_H
#define TEST_UI_H
#include <QWidget>
namespace Ui {
class test_ui;
}
class test_ui : public QWidget
{
Q_OBJECT
public:
explicit test_ui(QWidget *parent = nullptr);
~test_ui();
QTimer *timer;
public slots:
void button(int,bool);
private:
Ui::test_ui *ui;
signals:
};
#endif // TEST_UI_H
test_ui.cpp:
#include "test_ui.h"
#include "ui_test_ui.h"
test_ui::test_ui(QWidget *parent) :
QWidget(parent),
ui(new Ui::test_ui)
{
ui->setupUi(this);
}
test_ui::~test_ui()
{
delete ui;
}
void test_ui::button(int index, bool status){
qDebug() << "From test: " << status;
ui->status->setText((status) ? "On" : "Off");
}
My problem is that when I press the toggle button of user_ui's UI, the "on" or "off" status should display on the user UI and the test_ui. It displays on the user UI but not the test_ui. The qdebug line in the transmitter and the test class print but the UI of the test does not get updated so I am guessing that the function is getting called. So, I am not sure if it's a signal slot problem since the qdebug line is printing or if it's a timing problem.
I tested out my test UI by trying to print something in the QLineEdit box from a different function in that class and it works so there is no problem there.

I was able to solve my problem by simply adding my Signal and Slot connections from the 2 classes (user_ui & user_transmitter) to my main.cpp.

Related

setWindowState from QMainWindow from QWidget

I have a QMainWindow Application which also includes an QStackedWidget.
The pages of the QstackedWidget are promoted to ui widgets for example heating_widget.ui
If I use a button slot on my QMainWindow I can use this to get my application to fullscreen:
void SmartHome::on_fullscreen_on_clicked()
{
SmartHome::setWindowState(Qt::WindowFullScreen);
}
But how can I do this from a button which is in the heating_widget.cpp file?
Using:
void heating_widget::on_fullscreen_on_clicked()
{
SmartHome::setWindowState(Qt::WindowFullScreen);
}
obviously doesn't work and throws this error at me:
cannot call member function 'void
QWidget::setWindowState(Qt::WindowStates)' without object
SmartHome::setWindowState(Qt::WindowFullScreen);
I know this has something to do with parent() but I can't get it to work.
Do you have any idea?
My smarthome.h file:
#ifndef SMARTHOME_H
#define SMARTHOME_H
#include <QTime>
#include <QMainWindow>
namespace Ui {
class SmartHome;
}
class SmartHome : public QMainWindow
{
Q_OBJECT
public:
explicit SmartHome(QWidget *parent = 0);
~SmartHome();
private slots:
void on_Info_Button_clicked();
void on_News_Button_clicked();
void on_Heating_clicked();
void timerslot();
void on_Config_clicked();
void on_About_clicked();
public slots:
void setFullscreen();
private:
Ui::SmartHome *ui;
QTimer* myTimer;
};
#endif // SMARTHOME_H
My heating_widget.h :
#ifndef HEATING_WIDGET_H
#define HEATING_WIDGET_H
#include "smarthome.h"
#include <QWidget>
namespace Ui {
class heating_widget;
class SmartHome;
}
class heating_widget : public QWidget
{
Q_OBJECT
public:
explicit heating_widget(QWidget *parent = 0);
~heating_widget();
private slots:
void on_fullscreen_on_clicked();
private:
Ui::heating_widget *ui;
};
#endif // HEATING_WIDGET_H
and my heating.widget.cpp:
#include "heating_widget.h"
#include "ui_heating_widget.h"
#include "smarthome.h"
#include "iostream"
heating_widget::heating_widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::heating_widget)
{
ui->setupUi(this);
QObject::connect(ui->fullscreen_on, SIGNAL(clicked()), this , SLOT(SmartHome::setFullscreen()));
}
heating_widget::~heating_widget()
{
delete ui;
}
void heating_widget::on_fullscreen_on_clicked()
{
parentWidget()->setWindowState(Qt::WindowFullScreen);
std::cout<<"clicked"<<std::endl;
}
I would do it in the following way:
void heating_widget::on_fullscreen_on_clicked()
{
foreach(QWidget *widget, QApplication::topLevelWidgets()) {
if (auto mainWindow = qobject_cast<SmartHome *>(widget)) {
mainWindow->setWindowState(Qt::WindowFullScreen);
}
}
}
The idea is finding your main window among application top level widgets and change its state. This code can be called from anywhere in your application regardless of the windows hierarchy.

Qt - connecting a signal/slot in a triggered action

I'm using Qt 5 on a Windows and building a GUI App with multiple QDialog classes. I am trying to connect a signal from a QDialog in a triggered action of the QMainWindow class after instances of both have been created. I have read the documentation on Qt here: http://doc.qt.io/qt-4.8/signalsandslots.html and here: https://wiki.qt.io/New_Signal_Slot_Syntax. I have also read through many questions on stackoverflow that have helped correct some of the initial errors I was getting, but haven't helped me solve this problem.
The error I keep getting is:
"expected primary-expression before ',' token"
I have tried both the old syntax for connect
connect(sender, SIGNAL (valueChanged(QString,QString)),
receiver, SLOT (updateValue(QString)) );
and the new syntax (which is shown in the .cpp file below)
connect(sender, &Sender::valueChanged,
receiver, &Receiver::updateValue );
The MainWindow is created in the main.cpp and the 2nd dialog is created on_action_someAction_triggered(), so I know that the instances I am referencing exist. Is there a better way for me to connect the SIGNAL and the SLOT?
Here is the code I am working with (minus the extra unrelated code).
mainwindow .h:
#include <QMainWindow>
#include "shipdia.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void loadSelectedShip(QString shipName);
private slots:
void on_actionNew_Ship_triggered();
private:
Ui::MainWindow *ui;
shipdia *sDialog;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTextStream>
#include <QObject>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_actionNew_Ship_triggered()
{
sDialog = new shipdia(this);
QObject::connect(&shipdia, //this is were I attempt to
&shipdia::sendShip, //connect the signal/slot
this,&MainWindow::loadSelectedShip); //but always get an error
sDialog ->show();
}
void MainWindow::loadSelectedShip(QString shipName)
{
... do something ... //this code works, but the signal is never received
}
qdialog.h
#ifndef SHIPDIA_H
#define SHIPDIA_H
#include "functions.h"
#include <QDialog>
namespace Ui {
class shipdia;
}
class shipdia : public QDialog
{
Q_OBJECT
public:
explicit shipdia(QWidget *parent = 0);
~shipdia();
private slots:
void on_pushButton_2_clicked();
signals:
void sendShip(QString shipName);
private:
Ui::shipdia *ui;
};
#endif // SHIPDIA_H
qdialog.cpp
#include "shipdia.h"
#include "ui_shipdia.h"
#include <QObject>
#include <QMessageBox>
#include <QTextStream>
#include <QDir>
shipdia::shipdia(QWidget *parent) :
QDialog(parent),
ui(new Ui::shipdia)
{
ui->setupUi(this);
}
shipdia::~shipdia()
{
delete ui;
}
void shipdia::sendSelectedShip(QString shipName)
{
emit sendShip(shipName); //I previously just emitted sendSelectedShip,
//but separating the function did not fix it.
}
void shipdia::on_pushButton_2_clicked()
{
//Code below functions up to next comment
QString shipName = ui->line_23->text();
shipName = QDir::currentPath() + "/shipFolder/" + shipName + ".txt";
QFile shipFile(shipName);
QStringList stringList;
if (shipFile.open(QIODevice::ReadOnly))
{
QTextStream in(&shipFile);
while(!in.atEnd())
{
QString line = in.readLine();
if(line.isNull())
break;
else
stringList.append(line);
}
shipFile.close();
}
//Code above functions ^
sendSelectedShip(shipName); //this line does not produce an error
}
I think, the code should be
sDialog = new shipdia(this);
QObject::connect(sDialog,
&shipdia::sendShip,this,&MainWindow::loadSelectedShip);
and it should be placed in the constructor of the MainWindow, right after ui->setupUi(this); and the on_actionNew_Ship_triggered() function should look like this:
void MainWindow::on_actionNew_Ship_triggered()
{
sDialog ->show();
}
In your original code, a new instance of shipdia will be created everytime the on_actionNew_Ship_triggered() is called. That should be avoided.
Hope this helps.

Qt slot method (which is another class) not firing when emiting the signal

I know this question asked many times but still confused and cannot find a solution.
I have a MainWindow and a class.
In my class I have a signal which I emit in the method call.
Problem: Slot method is not firing.
Here is my code.
BWorker.h
#pragma once
#include <QObject>
class BWorker : public QObject
{
Q_OBJECT
public:
BWorker(QObject *parent);
~BWorker();
void doSomething();
signals:
void signalSomething();
};
BWorker.cpp
#include "BWorker.h"
BWorker::BWorker(QObject *parent)
: QObject(parent)
{
}
BWorker::~BWorker()
{
}
void BWorker::doSomething()
{
emit signalSomething();
}
QtGuiApplication1.h
#pragma once
#include <QtWidgets/QMainWindow>
#include "ui_QtGuiApplication1.h"
class QtGuiApplication1 : public QMainWindow
{
Q_OBJECT
public:
QtGuiApplication1(QWidget *parent = Q_NULLPTR);
public slots:
void workDone();
private:
Ui::QtGuiApplication1Class ui;
};
QtGuiApplication1.cpp
#include "QtGuiApplication1.h"
#include "BWorker.h"
QtGuiApplication1::QtGuiApplication1(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
BWorker bworker(this);
connect(&bworker, SIGNAL(bworker.signalSomething), this, SLOT(workDone()));
bworker.doSomething();
}
void QtGuiApplication1::workDone() {
}
Your problem is here:
SIGNAL(bworker.signalSomething)
This is not valid and you should see a message in terminal saying that there is no such signal. The correct syntax for your case would be:
SIGNAL(signalSomething())
Please post your code by copy-pasting it. Do not edit the code: The problem could be emerged from where you edited.
The code of mine worked fine.
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();
public slots:
void workDone();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include "worker.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
Worker worker(this);
connect(&worker, SIGNAL(signalSomething()), this, SLOT(workDone()));
//connect(&worker, &Worker::signalSomething, this, &MainWindow::workDone);
worker.doSomething();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::workDone()
{
qDebug() << "Done.";
}
worker.h:
#ifndef WORKER_H
#define WORKER_H
#include <QObject>
class Worker : public QObject
{
Q_OBJECT
public:
explicit Worker(QObject *parent = 0);
void doSomething();
signals:
void signalSomething();
public slots:
};
#endif // WORKER_H
worker.cpp:
#include "worker.h"
Worker::Worker(QObject *parent) : QObject(parent)
{
}
void Worker::doSomething()
{
emit signalSomething();
}
Application Output:
Debugging starts
Done.
Debugging has finished
Printed out "Done." means the codes are working.

Cannot execute a slot which should change GUI in Qt

I am trying to implement signal/slot connection which will show alert in editLine. I have no idea why the slot is not executing.
I tried: connect(mKlient, SIGNAL(showInfo(QString)), qApp, SLOT(aboutQt()));
To be sure that the signal is working correctly. So I think that something is wrong with the slots. Below is the code:
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <klient.h>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
Klient *mKlient;
private:
Ui::MainWindow *ui;
public slots:
void onShowInfo(QString);
private slots:
//void on_pushButton_clicked();
void on_pushButtonConnect_clicked();
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
onShowInfo("This alert is working"); // works
//-------------------Here is the signal/slot connection-------------------------------------
mKlient = new Klient(this);
connect(mKlient,SIGNAL(showInfo(QString)),this, SLOT(onShowInfo(QString))); //not working - no idea why :/
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::onShowInfo(QString text){
ui->lineEdit->setText(text);
}
void MainWindow::on_pushButtonConnect_clicked()
{
if(ui->radioButtonTCP->isChecked()){
mKlient->disconnect(this);
mKlient->connectToServer(ui->lineEditIpAddress->text(), ui->lineEditTcpPort->text().toInt());
}
}
klient.h
#ifndef KLIENT_H
#define KLIENT_H
#include <QObject>
#include <QTcpSocket>
#include <QAbstractSocket>
class Klient : public QObject
{
Q_OBJECT
public:
explicit Klient(QObject *parent = 0);
void connectToServer(QString,int);
bool isConnectedFlag;
signals:
void showInfo(QString);
public slots:
void connected();
private:
QTcpSocket *mSocket;
};
#endif // KLIENT_H
klient.cpp
#include "klient.h"
Klient::Klient(QObject *parent) :
QObject(parent)
{
isConnectedFlag = false;
}
void Klient::connectToServer(QString address, int port){
mSocket = new QTcpSocket(this);
connect(mSocket, SIGNAL(connected()),this,SLOT(connected()));
mSocket->connectToHost(address,port);
if(!mSocket->waitForConnected(1000)){
//error mSocket->errorString();
}
}
void Klient::connected(){
emit showInfo("Connected"); //this information should be on lineEdit
mSocket->write("siemanko"); //executing properly
isConnectedFlag = true;
}
}
I was following this tutorial to find a bug, but no results:
https://samdutton.wordpress.com/2008/10/03/debugging-signals-and-slots-in-qt/
Am I doing something wrong? I am new to Qt so maybe accessing to the GUI is more complicated than I thought.
I found the answer...
In
void MainWindow::on_pushButtonConnect_clicked()
{
if(ui->radioButtonTCP->isChecked()){
mKlient->disconnect(this);
mKlient->connectToServer(ui->lineEditIpAddress->text(), ui->lineEditTcpPort->text().toInt());
}
}
I thought that I am disconnecting TCP connection. Unfortunatelly I was disconnecting signal/slot connection.
Should I delete my question ? How to do this ?

How to access ui components from another class?

I am trying to call a method called display_txt() from my_class. This method is supposed to change some text in a label. But the method doesn't make any changes to the ui. I know the method runs due to the qDebug(). No errors occur, but the label just doesn't change to "text changed". display_txt() works fine and changes the label when called from the main widget class. There is a similar question in the forums but I couldn't use those solutions. I make an object when the button btn is pressed.
Here is the code:
widget.h
#include <QWidget>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
void display_txt();
private slots:
void on_btn_clicked();
private:
Ui::Widget *ui;
};
my_class.h
#include <QWidget>
#include "widget.h"
class my_class : public QWidget
{
Q_OBJECT
public:
explicit my_class(QWidget *parent = 0);
void test(Ui::Widget ui1);
signals:
private:
Widget *wo = new Widget;
};
Widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include "my_class.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
//display_txt();
qDebug() << "Widget object created";
}
void Widget::display_txt(){
ui->lbl->setText("text changes!!!!!");
qDebug() << "display_txt() method finished";
}
void Widget::on_btn_clicked()
{
my_class mc;
}
my_class.cpp
#include "my_class.h"
#include "QDebug"
my_class::my_class(QWidget *parent) : QWidget(parent)
{
qDebug() << "myclass ran";
wo->display_txt();
}