在 Qt 中不同类的 UI 标签上编写



我在我的项目中使用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(使用给定文本更改标签)的插槽。

最新更新