Although there are similar questions to mine posted on stackoverflow, none of their solutions actually respond to my problem. I have 2 independent widgets that I would like to combine (insert one widget into the other as a child): one is a UI created only with Qt Creator (drag-and-drop), and the other one an animation done in Qt with OpenGL. I am trying to add the animation in the UI and here is the code:
glwidget.h (animation):
class GLWidget : public QGLWidget
{
public:
GLWidget(QWidget *parent);
~GLWidget();
void initializeGL();
void resizeGL(int w, int h);
void paintGL();
void drawCube(int i, GLfloat z, GLfloat ri, GLfloat jmp, GLfloat amp);
QGLFramebufferObject *fbo;
};
and glwidget.cpp:
GLWidget::GLWidget(QWidget *parent)
: QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
{
makeCurrent();
fbo = new QGLFramebufferObject(512, 512);
timerId = startTimer(20);
}
GLWidget::~GLWidget()
{
glDeleteLists(pbufferList, 1);
delete fbo;
}
void GLWidget::initializeGL()
{....
As for the UI, I have the header file:
class ClaraTeCourseSimulator : public QMainWindow
{
Q_OBJECT
public:
explicit ClaraTeCourseSimulator(QWidget *parent = 0);
~ClaraTeCourseSimulator();
private:
Ui::ClaraTeCourseSimulator *ui;
GLWidget *defaultAnim;
protected:
void setupActions();
protected slots:
void addAnimWidget();
};
and the .cpp file:
ClaraTeCourseSimulator::ClaraTeCourseSimulator(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::ClaraTeCourseSimulator)
{
ui->setupUi(this);
defaultAnim = new GLWidget(ui->centralWidget);
}
void ClaraTeCourseSimulator::setupActions()
{
connect(ui->actionCar_Modelling, SIGNAL(triggered(bool)), ui->centralWidget,
SLOT(addAnimWidget()));
}
void ClaraTeCourseSimulator::addAnimWidget()
{
ui->centralWidget->layout()->addWidget(defaultAnim);
}
ClaraTeCourseSimulator::~ClaraTeCourseSimulator()
{
delete ui;
}
But when I try to run it I get about 24 of these errors: undefined reference to `imp_ZN9QGLFormatD1Ev all pointing to the constructor and destructor in glwidget.cpp.
What am I doing wrong? How can I fix this problem?
Are you trying to change the central widget to the GL one? Because specifying the parent for the GL widget does not change them. If you'd like to change a widget to another (using the Designer), I recommend the "promote to" feature, with which you can change a widget's actual class in the designer. So add a QWidget on the UI, and than promote it to your class (GLWidget).
It seems like the problem is with the GLFormat constructor call in the GLWidget constructor.
Related
I have followed this turorial Qt Creator + OpenGL and everything is going well.
I have added a QPlainTextEdit on top of the Quit button and I would like to have GLWidget send info back to MainWindow.
How can I make GLWidget call a function of the MainWindow?
Here is some code:
// GLWidget.h
class GLWidget: public QGLWidget
{
Q_OBJECT // <-----
public:
GLWidget(QWidget* parent = 0);
QWidget* parentObject;
void writeText( QString );
void (*p)(QString) = NULL;
protected:
virtual void initializeGL();
virtual void paintGL();
virtual void resizeGL(int w, int h);
private:
QTimer timer;
signals:
//void writeText( QString );
void dataReady(QString data); // <------------------
};
// GLWidget.cpp
GLWidget::GLWidget(QWidget* parent) : QGLWidget() /7 <---- new error
{
connect(&timer, SIGNAL(timeout()), this, SLOT(updateGL()));
timer.start(16);
QMessageBox msgBox;
msgBox.setText(typeid(parent).name());
msgBox.exec();
//p = parent->
//parentObject->
//parent->
//parent->
//this->parentObject = parent;
}
void GLWidget::initializeGL(){
//emit GLWidget::dataReady("myData"); //<--------- error
emit this->dataReady("Joe");//<--------- same as above
glClearColor(0.2, 0.2, 0.2, 1);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glEnable(GL_COLOR_MATERIAL);
}
// MainWindow.h
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
void print(QString text);
private slots:
void on_plainTextEdit_textChanged();
void foo(QString data); // <----------------------
}
// mainwindow.cpp
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->widget->p = &MainWindow::print; // error, how to asign pointer?
}
void MainWindow::print(QString text){
ui->plainTextEdit->setPlainText(text);
}
void MainWindow::foo(QString data) { // <---------
QMessageBox msgBox;
msgBox.setText("hi, there");
msgBox.exec();
// Do something with the data
ui->plainTextEdit->setPlainText(data);
}
error:
undefined reference to `vtable for GLWidget'
undefined reference to `GLWidget::dataReady(QString)'
Here is the answer to your immediate question, How to make a lower class exploit a function in it's parent?:
All QObject subclasses hold a reference to their parent object, accessible through the parent() method.
Include the header of the parent's class, e.g Foo.h
Call the parent() methodd to obtain a pointer to the object's parent
Check if the pointer is valid (not a nullptr)
Cast the pointer to the parent's class, e.g. Foo
Call the desired method, e.g. foo
In code it looks like this:
#include "Foo.h"
...
if (parent())
static_cast<Foo *>(parent())->foo();
That being said, this creates a tight coupling between the parent and the child and I strongly advise to avoid doing it if possible. In your case would be much better to use signals and slots to communicate the data from the child to the parent:
Define a signal in the child, e.g. dataReady(DataType data)
In the parent, when you create the child, connect this signal to the method, which will use the data, e.g. foo
In the child, emit the signal, whenever you feel like calling foo
In code it looks like this:
Child.h
...
signals:
void dataReady(DataType data);
...
Child.cpp
...
// I want to call foo
emt dataReady(myData);
...
Parent.h
...
private slots:
void foo(DataType data);
...
Parent.cpp
...
//somewhere
auto *child = new Child(...);
connect(child, &Child::dataReady, this, &Parent::foo);
...
void Parent::foo(DataType data) {
// Do something with the data
...
}
...
This requires more work initially, but it is much better in a long-term.
I create a very simple subclass of QWidget like this:
class WorldView : public QWidget
{
Q_OBJECT
public:
explicit WorldView(QWidget *parent = 0);
signals:
public slots:
};
WorldView::WorldView(QWidget *parent) : QWidget(parent)
{
}
I create an instance of it in main window like this:
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
protected:
virtual void resizeEvent(QResizeEvent* event) override;
private:
WorldView* _worldView;
};
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
_worldView = new WorldView(this);
_worldView->setStyleSheet(QString("* {background-color : black}"));
}
MainWindow::~MainWindow()
{
delete _worldView;
}
void MainWindow::resizeEvent(QResizeEvent *event)
{
_worldView->resize(size());
}
But the widget does not show as expected.
I have tried to call show(), but it still doesn't show.
The weird thing is that when I replace WorldView with QWidget, the widget shows.
I don't know why.
Because stylesheets don't work that way for custom QWidget subclasses.
From https://doc.qt.io/qt-5/stylesheet-reference.html :
QWidget
Supports only the background, background-clip and background-origin properties.
If you subclass from QWidget, you need to provide a paintEvent for your custom QWidget as below:
void CustomWidget::paintEvent(QPaintEvent *)
{
QStyleOption opt;
opt.init(this);
QPainter p(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}
The above code is a no-operation if there is no stylesheet set.
Warning: Make sure you define the Q_OBJECT macro for your custom widget.
(And, in general, stop using stylesheets.)
I wanted to create a simple application where there's triangle generated using OpenGL and three push buttons changing that triangle color. The triangle is generated but unfortunately buttons don't work and I get errors saying:
QObject::connect: No such slot MainWindow::redSlot(OGL) in
..\buttonwidget\mainwindow.cpp:17 QObject::connect: No such slot
MainWindow::greenSlot(OGL) in ..\buttonwidget\mainwindow.cpp:20
QObject::connect: No such slot MainWindow::blueSlot(OGL) in
..\buttonwidget\mainwindow.cpp:23
I have slots definitions:
void MainWindow::redSlot(Widget* w)
{
w->setColor(red);
}
void MainWindow::greenSlot(Widget* w)
{
w->setColor(green);
}
void MainWindow::blueSlot(Widget* w)
{
w->setColor(blue);
}
They are changing variable declared in class Widget that changes color of a generated triangle. Here's class 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);
};
And then I connect slots to buttons in class MainWindow constructor:
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);
}
Here's slot declaration in header:
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*);
};
How should I make them work?
Connects in QT are string-based, that means:
connect(redButton, SIGNAL(clicked()), this, SLOT(redSlot(OGL)));
will not work, since you havent defined a slot "redSlot(OGL)", instead you would have to use
...SLOT(redSlot(Widget*)));
Moreover, it is possible to define a SLOT with less parameter but not with more, therefore your slot/connect has to look like
void redSlot();
connect(redButton, SIGNAL(clicked()), this, SLOT(redSlot()));
if you need a pointer to the "Widget" you have to retrieve it from somewhere else.
Example: Define slot in "Widget"-class and change connect to:
connect(redButton, SIGNAL(clicked()), OGL, SLOT(redSlot()));
I'm quite new to Qt. I wanted to create a simple application where there's triangle generated using OpenGL and three push buttons changing that triangle colour. Unfortunately I get an error:
E:\Programy\Qt\5.3\mingw482_32\include\QtOpenGL\qgl.h:457: error: 'QGLWidget::QGLWidget(const QGLWidget&)' is private
Q_DISABLE_COPY(QGLWidget)
I don't know what to do. Here's my code:
MainWindow.h
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 w);
void greenSlot(Widget w);
void blueSlot(Widget w);
};
Slots in MainWindow.cpp look like this:
void MainWindow::redSlot(Widget w)
{
w.setColor(red);
}
Widget.h
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);
};
enum color is just an enum declared ina another header file
enum color
{
red,
green,
blue
};
Your Slots
void redSlot(Widget w);
void greenSlot(Widget w);
void blueSlot(Widget w);
all take a Parameter of Type Widget. This tries to create a copy of the object when called. Your Widget is a subclass of QGLWidget which has a private copy constructor and therefore can't be called from a subclassed object.
As you want to change the color of an existing object and not a copy of it you should change the functions to take a pointer of that object:
void redSlot(Widget*);
void greenSlot(Widget*);
void blueSlot(Widget*);
void MainWindow::redSlot(Widget* w)
{
w->setColor(red);
}
I am trying to implement a widget in Qt that has 2 child widgets of its own: one is a render area where I draw some points and connect lines between them and the other one is a ListBox where I would like to insert the list of all the points I drew with their coordinates from the render area. The 2 widgets where added with Qt Designer. Here is my code until now:
renderarea.h:
class RenderArea : public QWidget
{
Q_OBJECT
public:
RenderArea(QWidget *parent = 0);
QPoint point;
QList<QPoint> list;
protected:
void mousePressEvent(QMouseEvent *);
void paintEvent(QPaintEvent *event);
void updateList(QPoint p);
};
renderarea.cpp:
RenderArea::RenderArea(QWidget *parent)
: QWidget(parent)
{
setBackgroundRole(QPalette::Base);
setAutoFillBackground(true);
}
void RenderArea::mousePressEvent(QMouseEvent *e)
{
point = e->pos();
updateList(point);
this->update();
}
void RenderArea::updateList(QPoint p)
{
list.append(p);
}
void RenderArea::paintEvent(QPaintEvent * /* event */)
{
QPainter painter(this);
painter.setPen(QPen(Qt::black,2));
for (int i = 0; i < list.size(); ++i)
painter.drawPoint(list[i]);
if (list.size()>1)
for(int j = 0; j < list.size()-1; ++j)
painter.drawLine(list[j], list[j+1]);
}
paintwidget.h:
class PaintWidget : public QWidget
{
Q_OBJECT
public:
explicit PaintWidget(QWidget *parent = 0);
~PaintWidget();
private:
Ui::PaintWidget *ui;
};
paintwidget.cpp:
PaintWidget::PaintWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::PaintWidget)
{
ui->setupUi(this);
}
PaintWidget::~PaintWidget()
{
delete ui;
}
My question is how to transmit from the render area widget to my ListBox that I drew another point and it should be displayed along with its coordinates in the list?
The general approach used in QT development is using signal/slots for communication between components of software. So basically you need to define a signal in your source component (for instance RenderArea or whereever your like) and connect your slot defined in another component somewhere (i.e your Form Window) and fire a signal upon an action.
There are examples in the referenced link too.
OrcunC gave you a good advice.
If your are new to signal/slots implementation here some hints you can start from.
renderarea.h
signal:
void pointAdded(QPoint*);
renderarea.cpp
void RenderArea::updateList(QPoint p)
{
list.append(p);
emit pointAdded(&list.back());
}
listbox.h
public slots:
void onPointAdded(QPoint*);
listbox.cpp
void ListBox::onPointAdded(QPoint* point)
{
//lets assume your ListBox is a QListWidget
addItem( QString::number(point->x()) + "," + QString::number(point->y()))
}
somewhere instance of ListBox and RenderArea are accessible
QObject::connect( renderArea, SIGNAL(pointAdded(QPoint*),
listBox, SLOT(onPointAdded(QPoint*)));
NOTE: nameing is very important for readability and maintenance the void RenderArea::updateList(QPoint p) in this case it's more void RenderArea::addPoint( const QPoint& p) (also notice the const reference telling the compiler that we are not changing p event if we have it's reference)