Qt Designer - 如何将信号连接到静态函数? [英] Qt Designer - How to connect a signal to a static function?

查看:64
本文介绍了Qt Designer - 如何将信号连接到静态函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我正在使用 Qt Designer 来构建 GUI.我已经设法弄清楚如何制作 menuBar 并且我已经向栏添加了一些动作,但现在我需要连接这些动作以使它们做一些事情.

Okay I'm using Qt Designer to build a GUI. I've managed to figure out how to make the menuBar and I've added some actions to the bar, but now I need to connect the actions to make them do something.

具体来说,在我的文件菜单上,我有简单的打开操作.我希望此操作运行一个调用我的 QFileDialog 等的函数,但我不知道如何执行此操作.

Specifically, on my file menu, I have the simple open action. I want this action to run a function that calls my QFileDialog and so on, but I don't know how to do this.

那么,如何将 actionOpen 连接到静态函数?

So, how do I connect my actionOpen to my static function?

我使用的是最新的 Qt,5.0.2

I am using the latest Qt, 5.0.2

我在这里有点沮丧.这显然是某人可能需要做的最基本的事情之一,但我在网络上的任何地方都找不到任何真正的解决方案.从缺乏 Qt wiki 到其他人的问题,似乎没有人真正有明确的答案.旧版本的 Qt 有答案,但在那些旧版本中,信号显然无法连接到静态函数,所以这些都是无关紧要的.似乎没有人知道如何通过 Qt Designer 做到这一点.此外,也没有人明确将什么放在哪里.

I'm a little frustrated here. This is obviously one of the most basic things someone might need to do, yet I cannot find any real solution to this anywhere on the web. From the lacking Qt wiki, to other people's questions, nobody really seems to have a clear answer. There are answers for older versions of Qt, yet in those old versions apparently signals couldn't connect to static functions, so those are irrelevant. And nobody seems to know how to do this through the Qt Designer. Also, nobody ever clarifies where to put what.

我的 main.cpp 文件中有这一行:

I have this line in my main.cpp file:

QObject::connect(actionOpen, &actionOpen::triggered, fileOpen)

我在 Qt Designer 中创建了一个名为actionOpen"的对象,有一个名为 trigger 的信号,我在 main.cpp 中的 main 下方定义了一个名为fileOpen"的函数.这似乎遵循了正确的语法,但它引发了许多错误.

I have an object called 'actionOpen' made in Qt Designer, there is a signal called triggered, and I have a function defined just below my main inside main.cpp called 'fileOpen'. This seems to follow the proper syntax, yet it throws many errors.

此外,我可以在 Qt Creator 中反复单击构建,每次它都会出现不同数量的错误,消失和重新出现,我什至没有接触代码.我开始认为这个 IDE 有点像 POS.

Also, I can repeatedly click build in Qt Creator and every single time it comes up with a different number of errors, disappearing and reappearing, without me even touching the code. I'm starting to think this IDE is sort of a POS.

这是我的文件.也许这会有所帮助.

Here are my files. Maybe this will help somewhat.

主窗口.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();

public slots:
    void fileOpen();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

main.cpp

#include "mainwindow.h"
#include <QApplication>
#include <iostream>
#include <fstream>

using namespace std;


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

    w.show();


    return a.exec();
}

mainwindow.cpp

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <iostream>

using namespace std;



MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    QObject::connect(ui->actionOpen, &QAction::triggered, &MainWindow::fileOpen);
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;

}

void fileOpen()
{

    /*
    QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), QString(),
        tr("Text Files (*.txt);;C++ Files (*.cpp *.h)"));

    if (!fileName.isEmpty()) {
        QFile file(fileName);
        if (!file.open(QIODevice::ReadOnly)) {
            QMessageBox::critical(this, tr("Error"), tr("Could not open file"));
            return;
        }
        QTextStream in(&file);
        ui->textEdit->setText(in.readAll());
        file.close();
    }
    */

    cout << "Hello!";
}

推荐答案

第二个参数不正确.您应该指定类名,而不是对象名.所以应该是:

The second argument is incorrect. You should specify the class name, not object name. So it should be:

QObject::connect(actionOpen, &QAction::triggered, fileOpen);

完整的工作示例(已测试):

Complete working example (tested):

void fileOpen() {
  qDebug() << "ok";
}

int main(int argc, char *argv[]) {
  QApplication a(argc, argv);
  QMenu menu;
  QAction* actionOpen = menu.addAction("test");
  QObject::connect(actionOpen, &QAction::triggered, fileOpen);
  menu.show();
  return a.exec();
}

这篇关于Qt Designer - 如何将信号连接到静态函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆