简体   繁体   English

Qt C ++从另一个类访问窗口方法

[英]Qt C++ Accessing window methods from another class

Im using Qt for a project, I made a QWidget and it works fine. 我在项目中使用Qt,我做了一个QWidget,它工作正常。 In my widget class i have 3 methods, Quiz is my window,- 在我的小部件类中,我有3种方法,测验是我的窗口,-

void Quiz::appendConsole(QString string) {
    ui->console->append(string);
}

void Quiz::setInput(QString string) {
    ui->input->setText(string);
}

QString Quiz::getInput() {
    return ui->input->text();
}

and in the header class they are listed as public as shown - 并在标头类中将它们列为公共,如下所示:

namespace Ui {
class Quiz;
}

class Quiz : public QWidget
{
    Q_OBJECT

public:
     Quiz(QWidget *parent = 0);
    ~Quiz();
     void appendConsole(QString string);
     void setInput(QString string);
     QString getInput();

private slots:
    void on_button_clicked();

    void on_input_returnPressed();
private:
    Ui::Quiz *ui;
};

But when i try access any of these three methods from another class it fails with the error - error: call to non-static member function without an object argument 但是,当我尝试从另一个类访问这三个方法中的任何一个时,它将失败并显示错误-错误:调用无对象参数的非静态成员函数

void startQuiz() {
    Quiz::setInput("Hello");
}

does anyone know the proper way to access these classes or how these classes should be setup? 有谁知道访问这些类的正确方法或应如何设置这些类?

You are trying to call a function statically, even though it is not a static function. 您试图静态调用一个函数,即使它不是静态函数也是如此。 You need an object to call this function. 您需要一个对象来调用此函数。 Either pass your Quiz object to this other class, or use a signal. 将您的Quiz对象传递给另一个类,或者使用信号。

Option 1: Pass a reference of Quiz to your other class 选项1: Quiz的参考传递给您的其他班级

#include <QWidget>
#include <QLabel>
#include <QLayout>

class Quiz : public QWidget
{
    Q_OBJECT
public:
    Quiz(QWidget *parent = 0) : QWidget(parent) {
        setLayout(new QVBoxLayout);
        label = new QLabel;
        layout()->addWidget(label);
        resize(400, 400);
    }
    void setInput(const QString &input) {
        label->setText(input);
    }
private:
    QLabel *label;
};

class OtherClass : public QObject
{
    Q_OBJECT
public:
    OtherClass(QObject *parent = 0) : QObject(parent), quiz(0) {}
    void setQuiz(Quiz *quiz_ptr) {quiz = quiz_ptr;}
    void startQuiz() {
        if(quiz)
            quiz->setInput("Hello");
    }
private:
    Quiz *quiz;
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Quiz quiz;
    OtherClass otherClass;
    otherClass.setQuiz(&quiz);
    quiz.show();
    otherClass.startQuiz();
    return a.exec();
}

Option 2: Use a signal 选项2: 使用信号

#include <QWidget>
#include <QLabel>
#include <QLayout>

class Quiz : public QWidget
{
    Q_OBJECT
public:
    Quiz(QWidget *parent = 0) : QWidget(parent) {
        setLayout(new QVBoxLayout);
        label = new QLabel;
        layout()->addWidget(label);
        resize(400, 400);
    }
public slots:
    void setInput(const QString &input) {
        label->setText(input);
    }
private:
    QLabel *label;
};

class OtherClass : public QObject
{
    Q_OBJECT
public:
    OtherClass(QObject *parent = 0) : QObject(parent) {}
    void startQuiz() {
        emit changeInput("Hello");
    }

signals:
    void changeInput(const QString &input);
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Quiz quiz;
    OtherClass otherClass;
    QObject::connect(&otherClass, &OtherClass::changeInput, &quiz, &Quiz::setInput);
    quiz.show();
    otherClass.startQuiz();
    return a.exec();
}

First create an instance of the Quiz then you can call the method. 首先创建测验的实例,然后可以调用该方法。

Quiz *quiz = Quiz();
quiz->setInput("Hello");

The other possiblility is that you mean setInput to be a static method. 另一个可能性是您将setInput设为静态方法。

In which case declare it like this: 在这种情况下,应这样声明:

class  Quiz : public QWidget
{
    ...
    static setInput(QString string);
    ...
};

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

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