0

I have the following classes:

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QStringList pluginsToStart, QWidget *parent = 0);
    ~MainWindow();

// some other stuff

public slots:
    void on_timeDataChanged(logging::TimeValueVector<bool>& aData);
    void on_importStarted();
}

and

class DataImporterWidget : public PluginWidget
{
    Q_OBJECT

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

    void initConnections(QMap<QString, PluginWidget*> pluginWidgetMap);

in the method initConnections, I want the widget to init the signal-slot connections like so:

void DataImporterWidget::initConnections(QMap<QString, PluginWidget*> pluginWidgetMap)
{
    for(Importer* importer : this->getImporterMap().values())
    {
        connect(importer, SIGNAL(signal_timeDataChanged(logging::TimeValueVector<bool>&)),
            parentWidget(), SLOT(on_timeDataChanged(logging::TimeValueVector<bool>&)));
    }

    connect(this, SIGNAL(signal_importStarted()), parentWidget(), SLOT(on_importStarted()));
}

Importer is a QGroupBox and a base class for derived sub classes specifying concrete data importer types. It works like so: If I press a button, an DataImporterWidget is created and added to a QMdiArea as a QMdiSubWindow. When creating the DataImporterWidget I call the initConnections() method which sets up the signal-slot connections.

Now, when I run the program, I get the following message:

QObject::connect: No such slot 
QMdiSubWindow::on_timeDataChanged(logging::TimeValueVector<bool>&) in src/dataimporter/DataImporterWidget.cpp:81
QObject::connect: No such slot QMdiSubWindow::on_importStarted() in src/dataimporter/DataImporterWidget.cpp:85
QObject::connect:  (sender name:   'DataImporterWidget')

I do not understand why I get it because the slot is there. Even if I cast the parentWidget to the MainWindow, I get the same error.

PluginWidget is just a base class deriving from QWidget that holds some common functionality for my used plugins. I put Q_OBJECT on each base and derived class but still get this error. However, if I set up the connections in the MainWindow, it works just fine, but I wonder why the above solution won't work.

4

3 回答 3

0

我发现了问题。原因是,MainWidget 类包含一个 QMdiArea,我在其中添加了我的 PluginWidgets。因此,当我创建 PluginWidget 时,我将 MainWidget 设置为其父级,但一旦将其添加到 QMdiArea,它也成为 QMdiSubWindow 的子级。parentWidget 永远不会为空,但它是错误的......

于 2017-08-31T18:52:32.610 回答
0

您没有显示大量重要代码(例如创建DataImporterWidget、设置MainWindow为其父级、您调用的位置initConnections......)。然而,你说

如果我使用新的信号槽语法,我的程序会因分段错误而崩溃......

如果它崩溃了,那么您必须找到原因。使用旧的信号槽连接语法并不能治愈这种疾病,它只会延迟它的表现。据此,您获得段错误的原因可能是parentWidget() == nullptr尚未parent初始化。

DataImporterWidget我的建议是,检查您的代码,并在您的 call 之前创建和指定user 的父级initConnections()

于 2017-08-31T05:51:50.430 回答
0

不要从子对象创建连接,而是在创建子对象后从父对象代码创建它。这样你就不需要强制转换任何类型。

于 2017-08-30T18:29:13.590 回答