简体   繁体   English

如何从另一个类编辑QMainWindow UI窗口小部件?

[英]How do I edit QMainWindow UI Widget From Another Class?

I need to edit a QLabel from MainWindow's UI in another source file. 我需要在另一个源文件中从MainWindow的UI编辑QLabel。 I've tried messing around with singals and slots, but honestly I'm completely lost and the Qt documentation doesn't help me in this exact situation. 我曾经尝试过使用单数和插槽,但老实说我完全迷路了, Qt文档在这种情况下并没有帮助我。 I understand that this has been asked in the past, but I have not found a solution that works for me. 我知道过去曾有人问过这个问题,但我找不到适合我的解决方案。 I'm new to Qt, and C++ in general. 我是Qt和C ++的新手。

I have the following code (greatly simplified): 我有以下代码(大大简化了):

"mainwindow.h": “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();
    void setFoo(char* text);

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

"mainwindow.cpp" “mainwindow.cpp”

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow) {
    ui->setupUi(this);
    this->statusBar()->setSizeGripEnabled(false);
}

MainWindow::~MainWindow() {
    delete ui;
}

void MainWindow::setFoo(char* text) {
    ui->fooLabel->setText(text);
}

"secondwindow.h" “secondwindow.h”

#ifndef SECONDWINDOW_H
#define SECONDWINDOW_H

#include <QWidget>

namespace Ui {
class SecondWindow;
}

class SecondWindow: public QWidget {
    Q_OBJECT

public:
    explicit SecondWindow(QWidget *parent = 0);
    ~SecondWindow();

private:
    Ui::SecondWindow*ui;
}

#endif // SecondWindow_H

"secondwindow.cpp" “secondwindow.cpp”

#include "secondwinodw.h"
#include "ui_secondwinodw.h"
#include "mainwindow.h"

SecondWindow::SecondWindow(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SecondWindow) {
    ui->setupUi(this);
}

SecondWindow::~SecondWindow() {
    delete ui;
}

void SecondWindow::on_fooButton_clicked() {
    MainWindow::setFoo("example");//The method is private so this is not possible, but this is my goal
}

When a user clicks on fooButton, I need to access and edit the MainWindow's UI QLabel(or a public method that does this). 当用户单击fooButton时,我需要访问和编辑MainWindow的UI QLabel(或执行此操作的公共方法)。 The secondwindow is not being created in the main() function 在main()函数中未创建secondwindow

void MainWindow::keyPressEvent(QKeyEvent *event) {
    switch (event->key()) {
        case Qt::Key_A:
        if (event->modifiers()==Qt::ShiftModifier) {
            SecondWindow*secwind= new SecondWindow();
            secwind->show();
        }
        break;
    }
}

In OOP the ones that interact are the objects, not the classes or the files, that is, the following expression: I need to edit QLabel from MainWindow's UI in another source file It does not make sense, what you should say is that an object of the MainWindow class is modified by another object of the SecondWindow class. 在OOP中,交互的是对象,而不是类或文件,即以下表达式: 我需要在另一个源文件中的MainWindow的UI中编辑QLabel,这没有任何意义,您应该说的是一个对象MainWindow类的第二个对象被SecondWindow类的另一个对象修改。 The files do not make the program, the interaction between objects do it. 文件不是程序,对象之间的交互是程序。

I'm assuming that both objects are created in the main.cpp: 我假设两个对象都在main.cpp中创建:

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w1;
    SecondWindow w2;

    w1.show();
    w2.show();

    return a.exec();
}

Now if I go to attack the main problem, Qt handles the concept of signals and slots, so we will use it, as the action to a SecondWindow object will cause the information to be sent, I will create a signal that carries that information: 现在,如果我要解决主要问题,Qt将处理信号和插槽的概念,因此我们将使用它,因为对SecondWindow对象的操作将导致信息被发送,我将创建一个携带该信息的信号:

secondwindow.h secondwindow.h

#ifndef SECONDWINDOW_H
#define SECONDWINDOW_H

#include <QWidget>

namespace Ui {
class SecondWindow;
}

class SecondWindow: public QWidget {
    Q_OBJECT

public:
    explicit SecondWindow(QWidget *parent = 0);
    ~SecondWindow();
signals:
    void messageChanged(const QString & message); // <---
private slots:
    void void SecondWindow::on_fooButton_clicked();
private:
    Ui::SecondWindow*ui;
}

#endif // SecondWindow_H

when the button is pressed, the signal with the information must be emitted 当按下按钮时,必须发出带有信息的信号

secondwindow.cpp secondwindow.cpp

...
void SecondWindow::on_fooButton_clicked() {
    emit messageChanged("example");//The method is private so this is not possible, but this is my goal
}

Now we go to the receiver's side, how will we get a slots, you have done it but do not use char * , that's C, we're using C++ and much better we're using Qt, it's best to use QString : 现在我们转到接收方,我们将如何获得插槽,您已经完成了,但是不使用char * ,那就是C,我们正在使用C ++,更好的是我们正在使用Qt,最好使用QString

mainwindow.h mainwindow.h

....
void setFoo(const QString & text);

mainwindow.cpp mainwindow.cpp

...
void MainWindow::setFoo(const QString & text) {
    ui->fooLabel->setText(text);
}

And finally we make the connections: 最后我们建立连接:

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w1;
    SecondWindow w2;

    QObject::connect(&w2, &SecondWindow::messageChanged, &w1, &MainWindow::setFoo); 

    w1.show();
    w2.show();

    return a.exec();
}

update: 更新:

void MainWindow::keyPressEvent(QKeyEvent *event) {
    switch (event->key()) {
        case Qt::Key_A:
        if (event->modifiers()==Qt::ShiftModifier) {
            SecondWindow*secwind= new SecondWindow();
            connect(secwind, &SecondWindow::messageChanged, this, &MainWindow::setFoo);
            secwind->show();
        }
        break;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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