2014-10-30 88 views
-1

我想创建一个简单的应用程序,其中使用OpenGL生成三角形,并使用三个按钮更改三角形颜色。生成三角形但不幸的是按钮不工作,我得到的错误说:Qt,OpenGL Widget,没有这样的插槽

的QObject ::连接:没有这样的插槽的MainWindow :: redSlot(OGL)在 .. \ buttonwidget \ mainwindow.cpp:17 QObject :: connect:没有这样的插槽 MainWindow :: greenSlot(OGL)in .. \ buttonwidget \ mainwindow.cpp:20 QObject :: connect:没有这样的插槽MainWindow :: blueSlot(OGL)在 .. \ 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类中声明的变量,这些变量改变了生成的三角形的颜色。这里有一流的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(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*); 

}; 

我应该如何使他们的工作?

+0

你把声明之前的插槽关键字在'MainWindow'头,如果你没有做一个干净和重建 – 2014-10-30 10:20:20

+0

我用Qt已经有一段时间了,但我依稀记得这个错误。你是否正确使用MOC编译器?您是否正确定义了您的插槽(使用标题中的“public slot:”)? – kroneml 2014-10-30 10:21:23

+0

我建议你在遇到这样的问题之前学习一些Qt基础知识:http://qt-project.org/doc/qt-5/signalsandslots.html。如果你遇到了一些问题,你应该提供一个完整的代码的SSCCE,它完全描述你的问题,而不是从你的真实项目中发布一些代码片段。 – 2014-10-30 10:26:14

回答

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())); 

如果您需要指向“小工具”的指针,则必须从其他位置检索它。

例子:在“窗口小部件”级定义插槽和改变连接到:

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

那么我该如何影响我的小部件OGL?我需要改变存储在其中的变量的值,以改变三角形的颜色,我不能提出一个不同的解决方案。 – user30935 2014-10-30 10:28:22

+0

在您的“Widget”类中定义插槽而不是MainWindow,因此您可以直接访问相关数据。它也会更清洁(至少对我来说) – Robert 2014-10-30 10:38:37

+0

好的,这真的很有道理。我已经在我的Widget类中定义了我的插槽,但我仍然得到如下错误: 没有这样的插槽MainWindow :: greenSlot() 这是惊人的,因为greenSlot()不再在MainWindow类 – user30935 2014-10-30 10:50:59