简体   繁体   中英

QT Not Updating Second UI

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM