I am attempting to subclass a QLabel using a header file and I get the error on constructor
IntelliSense: indirect nonvirtual base class is not allowed
class foo : public QLabel
{
Q_OBJECT
foo(QWidget* parent = 0) : QWidget(parent)
{
}
void mouseMoveEvent( QMouseEvent * event )
{
//Capture this
}
};
Any suggestions why this is happening and how I can fix it ?
The problem is here:
foo(QWidget* parent = 0) : QWidget(parent)
You are inheriting from QLabel, but you specify QWidget for the base. You should write this intead:
explicit foo(QWidget* parent = Q_NULLPTR) : QLabel(parent)
// ^^^^^^
Also, please use explicit for that constructor as well as Q_NULL_PTR or at least NULL instead of 0.
Related
I have a problem regarding my QT project. I want to create a touchable Qlabel, so I created a placeholder in the gui designer and referenced it to my "watchlistlabel" which extends from QLabel. When I now start my app I get the error " no matching function for call to WatchListLabel::WatchListLabel(QGroupBox*&)'
name1 = new WatchListLabel(groupBox_3);
My labels are in a groupbox does this affect my code?
why it cant find a matching function?
^
#include<QLabel>
class WatchListLabel: public QLabel
{
Q_OBJECT
private:
void mousePressEvent(QMouseEvent * event) override;
public:
WatchListLabel();
};
watchlistlabel.cpp
#include "watchlistlabel.h"
void WatchListLabel::mousePressEvent(QMouseEvent *event)
{
}
WatchListLabel::WatchListLabel()
{
}
QTdesigner
You are missing a constructor which takes parent widget. General advice against this is to create new QObject subclasses using Qt Creator new class wizard, it will get everything right. But to solve the problem with this class, try replacing your default constructor with this constructor:
Declaration in .h:
explicit WatchListLabel(QWidget *parent = nullptr);
Definition in .cpp:
WatchListLabel::WatchListLabel(QWidget *parent)
: QLabel(parent)
//, any other member variable initializations
{
// any constructor code if needed
}
I have a question:
I have a class userinterface that has a class MoveSeries. From MoveSeries I want to have access to the methods of my class userinterface. In this example I want to have access to the method get_MoveCurve_Delta() of userinterface. How do I get access to the creating class (userinterface) from the created class (MoveSeries ? I tried the Signal-Slot-Approach but since I have to use several methods of userinterface several times this makes lots of signal-slots...
here is my code:
Userinterface.h:
class UserInterface : public QMainWindow
{
Q_OBJECT
public:
UserInterface(QWidget *parent = 0, Qt::WFlags flags = 0);
~UserInterface();
...
private:
double MoveCurve_Delta;
MoveSeries *MOVE_SERIES ;
public:
void set_MoveCurve_Delta( double val) { MoveCurve_Delta = val;}
double get_MoveCurve_Delta() { return MoveCurve_Delta ;}
}
Userinterface.cpp:
UserInterface::UserInterface(QWidget *parent, Qt::WFlags flags) :
QMainWindow(parent, flags)
{
ui.setupUi(this);
...
MOVE_SERIES = new MoveSeries( this);
}
MoveSeries.h:
class MoveSeries : public QDialog
{
Q_OBJECT
public:
explicit MoveSeries(QWidget *parent = 0);
~MoveSeries();
...
MoveSeries.cpp:
MoveSeries::MoveSeries(QWidget *parent) :
QDialog(parent),ui(new Ui::MoveSeries)
{
ui->setupUi(this);
this->parent = parent;
parent->set-MoveSeries_Delta_Val();
}
Rather than assume that the parent QWidget in MoveSeries is UserInterface, you can also require that it is.
MoveSeries.h:
class UserInterface; // only need a forward declaration
class MoveSeries : public QDialog
{
Q_OBJECT
public:
explicit MoveSeries(UserInterface *parent = 0);
~MoveSeries();
...
UserInterface * uiparent;
}
MoveSeries.cpp:
#include "Userinterface.h" // include the header where it is required
MoveSeries::MoveSeries(UserInterface *parent) :
QDialog(parent), ui(new Ui::MoveSeries), uiparent(parent)
{
ui->setupUi(this);
uiparent->set-MoveSeries_Delta_Val();
}
It looks like you want to cast the parent to the class you want:
static_cast<UserInterface *>(parent)->get_MoveCurve_Delta();
Bear in mind that this could be dangerous as it makes an assumption about the type of the parent.
If you want only UserInterface be the parent of MoveSeries, say so:
explicit MoveSeries(UserInterface *parent = 0);
If you want any widget to be able to act as the parent, you cannot access UserInterface methods because the parent does not necessarily have them.
In order to react to an event in the Qt framework I have reimplemented a Qt class (QLabel), i.e. its default constructor with a single argument. Since that gave an error, I also reimplemented the constructor with an additional argument. Now it works.
Class:
class myLabel : public QLabel
{
Q_OBJECT
public:
explicit myLabel(QWidget *parent = 0);
explicit myLabel(QString x, QWidget *parent = 0);
void mousePressEvent( QMouseEvent* e );
signals:
void clicked();
};
Constructors:
myLabel::myLabel(QWidget *parent) : QLabel(parent)
{
}
myLabel::myLabel(QString x, QWidget *parent) : QLabel(parent)
{
setText(x);
}
My (beginner) question is: why isn't the respective constructor of the base class run, as with all other methods [of the base class (e.g. I can use setText() without reimplementing it)]?
And the extreme: why do I need to reimplement a constructor at all, although it does not contain any code?
Edit:
Before posting, I was not sure what to search for, but this question has essentially been answered in https://stackoverflow.com/a/347362/1619432
Edit:
As bartimar pointed out, the constructor is not empty in that it calls the base class' constructor.
This can apparently be done with passing the respective parameters like so:
myLabel::myLabel( const QString& x, QWidget* parent, Qt::WindowFlags f ) :
QLabel( x, parent, f ) {}
I'm studying the Qt4 library and I want to add some functionality to all the children of QWidget in my project. I need all widgets to have mousePressEvent overridden. Obviously I do not need to override it in every particular widget I use(I use many of them and I they to follow DRY rule) , instead I create a Foo class:
class Foo : public QWidget
{
Q_OBJECT
protected:
/* implementation is just a test */
virtual void mousePressEvent(QMouseEvent*) { this->move(0,0); }
};
And derive my button from it:
class FooButton : public QPushButton , public Foo
{
public:
FooButton (QWidget* parent) : QPushButton(parent) { };
};
But the event seems to be not overridden... What am I doing wrong?
Thanks in advance.
For the mousePressEvent access, try QObject::installEventFilter().
http://doc.qt.digia.com/stable/qobject.html
You are inheriting twice now from QWidget. This is problematic (see diamond problem).
Instead, drop your Foo class and move your custom mouse press event handler to your FooButton class:
class FooButton : public QPushButton
{
Q_OBJECT
protected:
/* implementation is just a test */
virtual void mousePressEvent(QMouseEvent*) { this->move(0,0); }
public:
FooButton (QWidget* parent) : QPushButton(parent) { };
};
If this doesn't suit the design of your application, then try using virtual inheritance instead. That is, change Foo to:
class Foo : public virtual QWidget
If that doesn't help, you should follow the advice of the other answers and install an event handler.
(You can read about virtual inheritance in its Wikipedia article.)
I suspect that the problem is that you're inheriting from QWidget twice, once through QPushButton and once through Foo.
From the way you phrased the question, I'm assuming that you want to do this for varying kinds of widgets, and thus don't want to have to subclass QPushButton, QLabel, QCheckBox, etc. If this is not the case then you should use Nikos's answer.
If not, your best bet is probably doing to be to use an event filter.
class MousePressFilter : public QObject {
Q_OBJECT
public:
MousePressFilter(QObject *parent) : QObject(parent) { }
protected:
bool eventFilter(QObject *watched, QEvent *event) {
QWidget *widget = dynamic_cast<QWidget*>(watched);
widget->move(0,0);
return false;
}
};
And then in your Foo class constructor:
class Foo {
Foo() {
installEventFilter( new MousePressFilter(this) );
}
};
I have two class in Qt. In one I declared some variables and child QFrame class with QPainter. Now, if it's possible, how i can get access to parent variables from child class?
I know that I can pass variables by signals and slots or catch child QPainter events, but i think it would be nice get access directly.
It boils down to the visibility of the data in the base class. If the data is public or protected then you have access to it. Otherwise, the data is private and you don't have direct access to it.
Not exactly. Simple example:
header parent
class gameWindow : public QWidget
{
Q_OBJECT
public:
gameWindow(QWidget *parent = 0);
int round;
};
class parent
#include "gamewindow.h"
gameWindow::gameWindow(QWidget *parent) :
QWidget(parent)
{
round = 0;
}
header child :
class plArea:public QWidget
{
Q_OBJECT
public:
plArea(QWidget *parent=0);
};
class child:
#include "plarea.h"
plArea::plArea(QWidget *parent):QWidget (parent)
{
parent->round = 1;
}
return
'class QWidget' has no member named
'round'