在 Qt 中不同类的 UI 标签上编写
write on ui label from different class in qt
我在我的项目中使用Qt,从其他类访问u标签时遇到了一些困难,我有mainwindow和Yar类,如下所示。
主窗口.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "yar.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
Yar b;
private:
Ui::MainWindow *ui;
public slots:
void dispal();
private slots:
void on_pushButton_clicked();
};
#endif // MAINWINDOW_H
主窗口.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QObject::connect(ui->pushButton,SIGNAL(clicked()),&b,SLOT(lll()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::dispal(){
ui->label->setText("hello");
}
亚尔·
#ifndef YAR_H
#define YAR_H
#include <QObject>
class Yar : public QObject
{
Q_OBJECT
public:
explicit Yar(QObject *parent = 0);
public slots:
void lll();
};
#endif // YAR_H
void MainWindow::on_pushButton_clicked()
{//ui->label->setText("hello");
//b.wrrrot();
//dispal();
}
亚尔.cpp
#include "yar.h"
#include <iostream>
Yar::Yar(QObject *parent) :
QObject(parent)
{
//setupUi(this);
//QObject::connect(&a, SIGNAL(dis), &a, SLOT(dispal()));
//emit wrrrot();
}
void Yar::lll(){
//emit wrrrot();
std::cout<<"gfggdf"<<std::endl;
}
在我的 gui 中,我有一个按钮和一个标签,我已经将按钮与类 Yar 的 lll() 函数连接起来,当我单击按钮时,它在控制台中显示 gfggdf,但我想在 ui 标签中显示此文本,您能否帮助我如何从函数 lll() 在 ui 标签中显示我的数据;
一种可能的解决方案是从 Yar::lll() 发出信号,即
emit updateLabelText("your text");
并将此信号连接到主窗口中处理GUI(使用给定文本更改标签)的插槽。
相关文章:
- 如何在MS Visual Studio 2019中运行QT UI
- 如何处理使用.ui文件生成的.h文件
- 根据变量使Qt UI中的复选框为已选中/未选中
- C 和 C++ 中开关语句的案例标签的常量值,但显示不同的行为
- 一旦双簧管录制开始,主 UI 线程就会被阻止
- 如何正确指定 goto 语句的标签?
- Qt Quick,如何更改 Ui 源代码?
- 使用g++静态初始化带有命名标签的嵌套C++结构
- 通过水平滚动条更改标签
- QWidget UI 在使用 QQuickWidget 时冻结
- 在 3ds Max 中更新进度条后,环境和效果 UI 不刷新
- 从 QFontDatabase 设置 QFont 将所有标签设置为等宽字体?Qt C++
- Qt - QQuickWidget setSource without freezing UI
- 在C++中存储要输入的标签列表
- 如何在等效列表中查找最小的连接标签
- MFC/C++ ComboBox:禁用下拉列表关闭和打开(UI 冻结)的绘制
- Qt5 ui,多个窗口:如何从窗口 1 访问窗口 2 中的 UI 对象
- 标签检测鼠标单击 c++
- 在 Qt 中不同类的 UI 标签上编写
- 在UI上维护Qt标签和文本的好方法