Qt,OpenGL Widget,没有这样的插槽

时间:2014-10-30 10:15:02

标签: c++ qt opengl slot

我想创建一个简单的应用程序,其中使用OpenGL生成三角形,并使用三个按钮更改该三角形颜色。生成三角形,但不幸的是按钮不起作用,我得到错误说:

  

QObject :: connect:没有这样的插槽MainWindow :: redSlot(OGL)in   .. \ buttonwidget \ mainwindow.cpp:17 QObject :: connect:没有这样的插槽   MainWindow :: greenSlot(OGL)在.. \ buttonwidget \ mainwindow.cpp中:20   QObject :: connect:没有这样的插槽MainWindow :: blueSlot(OGL)in   .. \ buttonwidget \ mainwindow.cpp:23

我有插槽定义:

void MainWindow::redSlot(Widget* w)
{
    w->setColor(red);
}

void MainWindow::greenSlot(Widget* w)
{
    w->setColor(green);
}

void MainWindow::blueSlot(Widget* w)
{
    w->setColor(blue);
}

它们正在更改类Widget中声明的变量,该变量会更改生成的三角形的颜色。这里的课程小工具:

class Widget : public QGLWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = 0);

    QSize minimumSizeHint() const;
    QSize sizeHint() const;

    enum color c;

    void setColor(enum color color1);

protected:
    void initializeGL();
    void paintGL();
    void resizeGL(int width, int height);

};

然后我将插槽连接到MainWindow类构造函数中的按钮:

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
    layout = new QVBoxLayout();

    QWidget *w = new QWidget();
    setCentralWidget(w);

    w->setLayout(layout);

    Widget *OGL = new Widget();

    //OGL->c=green; - it was a test whether changing value of enum type variable c works
    //it works, changing it changes the color of a generated triangle

    redButton = new QPushButton(tr("Red"));
    connect(redButton, SIGNAL(clicked()), this, SLOT(redSlot(OGL)));

    greenButton = new QPushButton(tr("Green"));
    connect(greenButton, SIGNAL(clicked()), this, SLOT(greenSlot(OGL)));

    blueButton = new QPushButton(tr("Blue"));
    connect(blueButton, SIGNAL(clicked()), this, SLOT(blueSlot(OGL)));

    layout->addWidget(OGL);
    layout->addWidget(redButton);
    layout->addWidget(greenButton);
    layout->addWidget(blueButton);
}

标题中的插槽声明:

class MainWindow : public QMainWindow
{
    Q_OBJECT

    QVBoxLayout *layout;

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


private:
    QPushButton *redButton;
    QPushButton *greenButton;
    QPushButton *blueButton;

public slots:
    void redSlot(Widget*);
    void greenSlot(Widget*);
    void blueSlot(Widget*);

};

我应该如何让它们发挥作用?

1 个答案:

答案 0 :(得分:1)

QT中的连接是基于字符串的,这意味着:

connect(redButton, SIGNAL(clicked()), this, SLOT(redSlot(OGL)));

不起作用,因为你没有定义一个插槽“redSlot(OGL)”,而你必须使用

...SLOT(redSlot(Widget*)));

此外,可以定义一个参数较少但没有更多的SLOT,因此您的插槽/连接必须看起来像

void redSlot();

connect(redButton, SIGNAL(clicked()), this, SLOT(redSlot()));

如果你需要一个指向“Widget”的指针,你必须从其他地方检索它。

示例:在“Widget”类中定义槽并将connect更改为:

connect(redButton, SIGNAL(clicked()), OGL, SLOT(redSlot()));