QWidget通过一个插槽

时间:2016-06-02 07:56:03

标签: qt signals signals-slots qwidget qt-signals

我试图制作一个功能,根据传递给它的QWidget显示一个小部件。

我有:

position_widget = new positionWidget();
timing_widget = new timingWidget();
...

void MainWindow::showScreen(QWidget *w)
{
    ui->screenWidget->layout()->addWidget(w);
    w->show();
}


void MainWindow::doConnects()
{
QObject::connect(buttons_widget, SIGNAL(showPositionScreen_signal()), this, SLOT(showScreen(position_screen)));
QObject::connect(buttons_widget, SIGNAL(showTimingScreen_signal()), this, SLOT(showScreen(timing_screen)));
}

单击按钮后没有任何反应,它出现了“没有这样的插槽MainWindow :: ShowScreen(timing_screen)'

1 个答案:

答案 0 :(得分:1)

如果Qt Slot中的mainwindow.h被声明为private slots: void showScreen(QWidget* w); ,请执行以下操作:

buttons_widget

您的信号在signals: void showPositionScreen_signal(QWidget* w); //Note that signal needs same type as slot void showTimingScreen_signal(QWidget* w);

中宣布
connect(buttons_widget, SIGNAL(showPositionScreen_signal(QWidget*)), this, SLOT(showScreen(QWidget*)));

然后您可以将该信号连接到插槽。请注意,信号和插槽的参数必须匹配。 即:"The signals and slots mechanism is type safe: The signature of a signal must match the signature of the receiving slot. (In fact a slot may have a shorter signature than the signal it receives because it can ignore extra arguments.)"

position_screen

你必须从timing_screen内发出buttons_widgetemit showPositionScreen_signal(position_screen); ,如:

QWidget

正如 thuga 指出的那样,就是说你不需要两个不同的信号。要将另一个emit showPositionScreen_signal(timing_screen); 传递到同一个插槽,只需用它发出该信号即可。即:

    public float speed = 0.1F;
    private float rotation_x;
    void Update()
    {
        if (Input.GetButtonDown("Fire1"))
        {
            rotation_x = transform.rotation.eulerAngles.x;
            rotation_x += 180;
        }
        transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(rotation_x, transform.eulerAngles.y, transform.eulerAngles.z), Time.time * speed);

    }

我建议将信号名称改为适当的名称。