[英]QT Not Updating Second UI
I have 2 UIs and I am trying to update one from the other.我有 2 个 UI,我正在尝试从另一个更新一个。 I have the classes phone
, user_ui
, test_ui
, and user_transmitter
.我有类phone
、 user_ui
、 test_ui
和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. user_ui
是用户可以与之交互的那个,例如如果用户按下 user_ui 的 UI 上的切换按钮,那么状态应该显示在 test_ui 的 UI 上。 The user_transmitter transmits the inputs and outputs and that's its only job, there is no UI to it. user_transmitter 传输输入和输出,这是它唯一的工作,它没有 UI。 The phone class just contains the variables and nothing else.电话 class 只包含变量,没有其他内容。
user_ui.h: 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: 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: 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: 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: 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: 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.我的问题是,当我按下 user_ui 的 UI 的切换按钮时,“on”或“off”状态应该显示在用户 UI 和 test_ui 上。 It displays on the user UI but not the test_ui.它显示在用户 UI 上,但不显示在 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.变送器中的 qdebug 行和测试 class 打印,但测试的 UI 没有更新,所以我猜测正在调用 function。 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.所以,我不确定它是否是信号槽问题,因为 qdebug 行正在打印,或者它是否是时间问题。
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.我通过尝试从 class 中的不同 function 在 QLineEdit 框中打印一些内容来测试我的测试 UI,它可以工作,所以那里没有问题。
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.我能够通过简单地将来自 2 个类(user_ui 和 user_transmitter)的信号和插槽连接添加到我的 main.cpp 来解决我的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.