我想创建一个简单的应用程序,其中使用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*);
};
我应该如何使他们的工作?
你把声明之前的插槽关键字在'MainWindow'头,如果你没有做一个干净和重建 – 2014-10-30 10:20:20
我用Qt已经有一段时间了,但我依稀记得这个错误。你是否正确使用MOC编译器?您是否正确定义了您的插槽(使用标题中的“public slot:”)? – kroneml 2014-10-30 10:21:23
我建议你在遇到这样的问题之前学习一些Qt基础知识:http://qt-project.org/doc/qt-5/signalsandslots.html。如果你遇到了一些问题,你应该提供一个完整的代码的SSCCE,它完全描述你的问题,而不是从你的真实项目中发布一些代码片段。 – 2014-10-30 10:26:14