简体   繁体   English

类之间的QT连接

[英]QT Connection between classes

I think i have some troubles getting this right: I have a QMainWindow class. 我认为我在解决这个问题时遇到了一些麻烦:我有一个QMainWindow类。 In my programm I want to create other classes eg for input handling, computation... 在我的程序中,我想创建其他类,例如用于输入处理,计算...

Now first from my mainwindow class i want to send to my fileselector (file handler) class to open a file dialog, thus save the selected files internally. 现在,首先要从我的mainwindow类发送到我的fileselector(文件处理程序)类以打开文件对话框,从而在内部保存选定的文件。 Unfortunately I am having troubles to connect the slots. 不幸的是,我在连接插槽时遇到麻烦。

main window: 主窗口:

MA_FEX::MA_FEX(QWidget *parent)
    : QMainWindow(parent), fileSelector(this)

{
    ui.setupUi(this);
    //this works:
    fileSelector.openFiles(this); 
    //this doesn't:
    connect(ui.actionOpenFiles, SIGNAL(triggered()), fileSelector, SLOT(openFiles(this)));
}

MA_FEX::~MA_FEX()
{  
}

mainwindow header: 主窗口标题:

class MA_FEX : public QMainWindow
{
    Q_OBJECT

public:
    MA_FEX(QWidget *parent = 0);
    ~MA_FEX();

private:
    Ui::MA_FEXClass ui;
    FileSelection fileSelector;
};

file coordination class: 文件协调类:

FileSelection::FileSelection(QObject *parent)
    : QObject(parent)
{
}

FileSelection::~FileSelection()
{
}

void FileSelection::openFiles(QWidget *parent){

    QStringList files = QFileDialog::getOpenFileNames(
                         parent,
                         "Select one or more files to open",
                         "c:",
                         "Images (*.csv *.txt )");

}

header: 标题:

class FileSelection : public QObject
{
    Q_OBJECT

public:
    FileSelection(QObject *parent);
    ~FileSelection();

public slots:
    void openFiles(QWidget *parent);

private:

};

Am I missing something ? 我想念什么吗? Executing i get Error C2664 on the connect line saying that Parameter 3 'FileSelection' cannot be converted to 'const QObject'. 执行我在连接线上收到错误C2664,说参数3'FileSelection'无法转换为'const QObject'。

Look at the declaration of the QObject::connect : 查看QObject::connect的声明:

QObject::connect(const QObject * sender, const char * signal,
                 const QObject * receiver, const char * method,
                 Qt::ConnectionType type = Qt::AutoConnection);

It takes pointers, so you need to pass a pointer to fileSelector . 它需要指针,因此您需要将指针传递给fileSelector

Another problem there is incompatible SIGNAL and SLOT. 另一个问题是信号和插槽不兼容。 The slot specification in connect is declaration, so you cannot pass arguments as you did with this . connect中的插槽规范是声明,因此您不能像this那样传递参数。 If you use Qt 5 and C++11 you can do that by passing lambda instead of slot specification: 如果使用Qt 5和C ++ 11,则可以通过传递lambda而不是插槽规范来实现:

QObject::connect(ui.actionOpenFiles, &QAction::triggered, 
                [this]() { fileSelector.openFiles(this); });

For Qt 4 you need to create wrapping slot in your MA_FEX class which takes no arguments and which will invoke the slot of the fileSelector : 对于Qt 4,您需要在MA_FEX类中创建包装槽,该包装槽不使用任何参数,并且将调用fileSelector的槽:

class MA_FEX {
  ...
  Q_SLOT void openFileSelector() { fileSelector.openFiles(this); }
  ...
public:
  MA_FEX(QWidget *parent) : QMainWindow(parent), fileSelector(this) {
    ui.setupUi(this);
    connect(ui.actionOpenFiles, SIGNAL(triggered()), SLOT(openFileSelector()));
  }
  ...
};

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

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