create object for class inheriting QLayout - qt

I have a screen class as
class Screen : public QLayout
{
public:
Screen();
~Screen();
void paintEvent(QPaintEvent *e);
};
When I am creating the object I got an error that can not create an object for pure abstract class. Since QLayoput is pure abstract , How can I create an object for a class which is inherits the QLayout ?
definitions:
Screen::Screen( )
{
}
Screen::~Screen()
{
delete this ;
//Screen(new QSize (100,100));
}
void Screen::paintEvent(QPaintEvent *e)
{
}

QLayout is pure abstract, meaning it has virtual members without a definition. To subclass it, you need to provide definitions for all such methods in your class. Specifically, Qt Docs state that
To make your own layout manager, implement the functions addItem(),
sizeHint(), setGeometry(), itemAt() and takeAt().
For more, see there (there are additional optional advices for further functions which should be implemented): http://qt-project.org/doc/qt-4.8/qlayout.html

Related

QObject connect fails in Qt

everyone!
I am having a problem using QObject::connect with some custom classes I've created. First of all, I have created 2 classes that inherit from QObject, they are called: Valve and PushButton. They are instantiated inside controllers called PanelController and SynopticController, which are also QObjects. And these controllers are instantiated inside another class called MasterController, also a QObject. I find this information useful since I think it is a problem of referencing the classes or the way I might be instantiating my classes inside these controllers. I strongly think this, because in my main method, when I do the following snippet of code, the connection works:
...
avionics::synoptic::Valve valveTest(nullptr, avionics::synoptic::ValveName::ABV);
avionics::panel::PushButton pushButtonTest(nullptr, avionics::panel::PanelNames::RECIRC);
QObject::connect(&pushButtonTest, &avionics::panel::PushButton::onStateColorChanged, &valveTest, &avionics::synoptic::Valve::updateState);
...
Basically, the controller classes are:
// MasterController
class MasterController : public QObject {
...
private:
panel::PanelController* panelController{nullptr};
synoptic::SynopticController* synopticController{nullptr};
}
// Panel Controller
class PanelController : public QObject {
...
explicit PanelController(QObject *parent = nulptr){
this->pushButtons.append(new avionics::panel::PushButton(_panelController, avionics::panel::PanelNames::RECIRC));
}
private:
QList<avionics::panel::PushButton*> pushButtons{};
}
// SynopticController
class SynopticController : public QObject {
private:
QList<avionics::synoptic::Valve*> iceValves{};
explicit SynopticController(QObject *parent = nullptr) {
antiIcePneumaticLines.append(new avionics::synoptic::PneumaticLine(_synopticController, avionics::synoptic::PneumaticLineName::APU_2_ABV));
}
}
My problem is that when I do the same call for the QObject::connect either from my MasterController constructor or my main method, the signal doesn't call the slot function. I want to connect pushButtons to valves, and to do this I am using getters from my controllers. The call to QObject::connect that doesn't work is:
QObject::connect(panelController->getpushButtons().at(1), &avionics::panel::PushButton::onStateColorChanged, synopticController->getValves().at(1), &avionics::synoptic::Valve::updateState);
// Example of getter
QList<avionics::panel::PushButton*> PanelController::getPushButtons(){
return pushButtons;
}
I've put some prints inside the method that emits the signal and tried debugging it, but the signal is emitted and the slot isn't called. The classes return from the getters are not undefined or null, I've checked it. Let me know if something wasn't clear. Thanks in advance!

How to use protected function setLocalPort?

I should use setlocalport for my socket connection but the property is protected and i have an error of compilation.
This is in qt application.
m_pSocket = new QTcpSocket();
m_pSocket->setLocalPort(m_iLocalPort);
error: ‘void QAbstractSocket::setLocalPort(quint16)’ is protected
If you want to use protected member like a public one, then you should provide a custom class that is the child of the class whose protected method you intend to use. There is nothing that would forbid you to create a child class inheriting QTcpSocket, and then using the protected method you want. Example for the QTcpSocket case that has been described here can be the following.
// Let us define CustomTcpSocket, i.e. the class inheriting QTcpSocket
#pragma once
#include <QTcpSocket>
class CustomTcpSocket
: public QTcpSocket
{
Q_OBJECT
public:
CustomTcpSocket(QObject* parent = nullptr);
virtual ~CustomTcpSocket();
// This method will be used to call QTcpSocket::setLocalPort which is protected.
void SetLocalPort(quint16 port);
};
Then, we provide the implementation itself.
#include "CustomTcpSocket.h"
CustomTcpSocket::CustomTcpSocket(QObject* parent)
: QTcpSocket(parent)
{
}
CustomTcpSocket::~CustomTcpSocket()
{
}
void CustomTcpSocket::SetLocalPort(quint16 port)
{
// Since method is protected, and scope is the child one, we can easily call this method here.
QAbstractSocket::setLocalPort(port);
}
Now we can easily use this newly created class in the following way.
auto customTcpSocketInstance = new CustomTcpSocket();
customTcpSocketInstance->SetLocalPort(123456);
Through usage of polymorphism, instances of CustomTcpSocket should be accepted by other Qt's APIs. However, there is no guarantee it will work as you would expect it to. Qt developers wanted this method to be protected for some of the reasons. So, use it with caution.

Qt sending data from parent to child widget

I have a 2 widgets inherited from QDialog. One of these widgets is called by another widget.
I need to pass data from the parent widget to the child. For example, I want passing QStringList.
I can make signals and slots in both classes. Slot of parent widget class - transferList(QStringList) - filling my QStringList.
How should I make the signal and slot connection? The child widget, of course, knows nothing about the parent.
// .h-file of parent widget.
class ElectricIndicationDialog : public QDialog {
#include "PollIndication.h" // class of child widget
QSharedPointer <PollIndication> pollInd;
public slots:
void transferList(QStringList);
signals:
void listTfansfer(QStringList);
private:
QStringList sendList;
};
// .cpp-file of parent widget
pollInd = QSharedPointer <PollIndication>(new PollIndication());
pollInd->show();
void ConfIndication::transferList(QStringList lst) {
lst.append("str1");
lst.append("str2");
}
// .h-file of child widget
class PollIndication : public QDialog {
public slots:
void getList(QStringList);
signals:
void listGet(QStringList);
private:
QStringList recList; // We transfer data to it
}
You don't need a signal/slot for that: your parent knows the type of its child and has a pointer on it. So, you can call a method of PollIndication when you need to send data to your dialog.
void ConfIndication::transferList(QStringList lst) {
lst.append("str1");
lst.append("str2");
pollInd->changeTransferList(lst);
}
If your dialog is modal, you can also create your dialog only when needed and give your list as parameter of the constructor.
void ConfIndication::transferList(QStringList lst) {
lst.append("str1");
lst.append("str2");
PollIndication* pollInd = new PollIndication(lst, this);
pollInd->exec();
}
It is normally a bad idea to make a parent class to know what are their children....
you can in the parent class define an abstract method (think about some pure virtual) so every childclass is forced to implement it... after that, the parent class can invoke the method and the child will implement the login depending on how it must react to it...

Is it feasible to use decorator pattern with QWidget?

According to wikipedia, I need to
In the Decorator class, redirect all "Component" methods to the
"Component" pointer;
But QWidget has too many methods to redirect.
For example I want to create a moveable widget decorator:
class Moveable : public QWidget
{
Q_OBJECT
public:
explicit Moveable(QWidget * widgetToBeDecorated){
this->widgetToBeDecorated = widgetToBeDecorated;
}
~Moveable();
protected:
void mousePressEvent(QMouseEvent* e) override {
lastPoint = e->pos();
widgetToBeDecorated->mousePressEvent(e);
}
void mouseMoveEvent(QMouseEvent* e) override {
auto offset = e->pos()-lastPoint;
lastPoint = e->pos();
widgetToBeDecorated->move(widgetToBeDecorated->pos()+offset);
widgetToBeDecorated->mouseMoveEvent(e);
}
void mouseReleaseEvent(QMouseEvent* e) override {
widgetToBeDecorated->mouseReleaseEvent(e);
}
private:
QWidget* widgetToBeDecorated;
QPoint lastPoint;
};
Do I need to redirect other methods like show(), paintEvent(QPaintEvent*)?
The decorator pattern is a guideline, not a rule. You are fully allowed to modify patterns that do not suit your purposes. In this case, you only need to concern yourself with the part of QWidget that are involved with positioning, so there is no advantage to overriding any other of it's methods.
It looks like you only want to modify the behaviour of certain events in your QWidget. Lucky for you, there is already a mechanism in QObject to do just such a thing, it's called an Event Filter.
There is a nice example of how to use it in the Qt documentation, and a good advantage that improves on your class above is that you can use the same object to filter many QWidgets, instead of having one filter for each widget.

Derive UIC generated Qt UI class from custom interface

I've a simple Qt Question. I want that automatically generated UIC files are derived from a custom interface class like in:
Intention
class MyUiInterface {
public:
virtual void setupUi(QWidget* w) = 0;
virtual void retranslateUi(QWidget*w) = 0;
};
Generated UIC file should look like:
class Ui_MyWidget {
public:
void setupUi(QWidget* w) {
...
}
void retranslateUi(QWidget* w) {
...
}
};
namespace Ui {
class MyWidget : public MyUiInterface , public Ui_MyWidget {};
}
Why?
Every Ui::Class would then implement MyUiInterface. In each class that derives from Ui::Class (see The Multiple Inheritance Approach) I would be able to call setupUi and retranslateUi which makes sense if the class that derives from UI::Class class is a base class either. I want every widget to be derived from my abstrcat base class MyWidgetBase. Consider following:
class MyWidgetBase abstract : public QWidget, protected MyUiInterface {
protected:
void changeEvent(QEvent *e) {
QWidget::changeEvent(e);
if (e->type() == QEvent::LanguageChange) {
retranslateUi(this); // Still abstract here
}
}
};
class MyWidget : public MyWidgetBase : public Ui::MyWidget {
};
The effect is, every time MyWidget::changeEvent() is callled, retranslateUi of that specific class is called. Otherwise changeEvent had to be reimplemented in each class. This would be a bit against "code reuse" concept.
I think Qt UIC is not able to handle this situation isn't it? Is there a similar way to solve this problem?
Unfortunately, reading XML Schema for ui files is telling us that this is not possible to automate using uic compiler.
However, it is unclear to me why you would want to implement that automatically - even if the Uic somehow manages to implement your interface, you will still need to add bodies of the functions by hand, editing generated .h file, as I am sure that there is no way to include custom code in xml file which will translate as C++ code.
Why you just don't reimplement setupUi and retranslateUi in your MyWidget class? Every Ui class will have one of these classes, so you can implement this on this level, instead of base class. It is possible that I am missing something, but I see this as an appropriate way to do this.
class MyWidget : public MyWidgetBase, public Ui::MyWidget {
public:
void setupUi(QWidget* w) {
...
}
void retranslateUi(QWidget* w) {
...
}
};
With this approach, you don't need to reimplement changeEvent() in any of your custom widgets, and changeEvent will still call the appropriate retranslateUi().

Resources