Subclassing QLabel and use it in QWidget class - c++

When i try to put QLabel in QWidget class its not work properly (no hover event or click event only the label pixmap is show) only the last instance work properly, when not use set parent, it create in new window for each label but its work correctly
this gif show the problem:
https://media.giphy.com/media/3o7TKKmZSISGXN4Opq/giphy.gif
this is QLabel subclass header:
#include <QObject>
#include <QLabel>
class myLabel : public QLabel
{
Q_OBJECT
public:
myLabel();
protected:
void mousePressEvent(QMouseEvent *);
void enterEvent(QEvent *);
void leaveEvent(QEvent *);
signals :
void labelClicked();
void enterSignal();
void leaveEventSignal();
private:
};
this class to make a labelButton:
#include <QObject>
#include <QWidget>
#include "mylabel.h"
class labelButton : public QWidget
{
Q_OBJECT
public:
labelButton();
//some functions
private slots:
//slots
private:
//private member
};
and this the class that i want to use the labelButtons in:
#include <QWidget>
#include "labelbutton.h"
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private:
Ui::Widget *ui;
labelButton *b_1, *b_2, *b_3;
};
here is widget.cpp:
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
b_1 = new labelButton;
b_1->setParent(this);
b_1->moveButton(70, 100);
//some functions to initialize the labelButton
b_1->show();
//-----------------------
b_2 = new labelButton;
b_2->setParent(this);
b_2->moveButton(70, 200);
//some functions to initialize the labelButton
b_2->show();
//-----------------------
b_3 = new labelButton;
b_3->setParent(this);
b_3->moveButton(70, 300);
//some functions to initialize the labelButton
b_3->show();
}

here its work, the problem was in passing the parent
i made a function that take a widget and set buttons parent from the function value
b_1 = new labelButton;
//b_1->setParent(this);
b_1->setParentFunc(this);
b_1->moveButton(70, 100);
//some functions to initialize the labelButton
// b_1->show();
in labelButton:
void labelButton::setParentFunc(QWidget *p)
{
myParent = p;
}
mLabel_1->setParent(myParent); // myParent instead of this

Related

setWindowState from QMainWindow from QWidget

I have a QMainWindow Application which also includes an QStackedWidget.
The pages of the QstackedWidget are promoted to ui widgets for example heating_widget.ui
If I use a button slot on my QMainWindow I can use this to get my application to fullscreen:
void SmartHome::on_fullscreen_on_clicked()
{
SmartHome::setWindowState(Qt::WindowFullScreen);
}
But how can I do this from a button which is in the heating_widget.cpp file?
Using:
void heating_widget::on_fullscreen_on_clicked()
{
SmartHome::setWindowState(Qt::WindowFullScreen);
}
obviously doesn't work and throws this error at me:
cannot call member function 'void
QWidget::setWindowState(Qt::WindowStates)' without object
SmartHome::setWindowState(Qt::WindowFullScreen);
I know this has something to do with parent() but I can't get it to work.
Do you have any idea?
My smarthome.h file:
#ifndef SMARTHOME_H
#define SMARTHOME_H
#include <QTime>
#include <QMainWindow>
namespace Ui {
class SmartHome;
}
class SmartHome : public QMainWindow
{
Q_OBJECT
public:
explicit SmartHome(QWidget *parent = 0);
~SmartHome();
private slots:
void on_Info_Button_clicked();
void on_News_Button_clicked();
void on_Heating_clicked();
void timerslot();
void on_Config_clicked();
void on_About_clicked();
public slots:
void setFullscreen();
private:
Ui::SmartHome *ui;
QTimer* myTimer;
};
#endif // SMARTHOME_H
My heating_widget.h :
#ifndef HEATING_WIDGET_H
#define HEATING_WIDGET_H
#include "smarthome.h"
#include <QWidget>
namespace Ui {
class heating_widget;
class SmartHome;
}
class heating_widget : public QWidget
{
Q_OBJECT
public:
explicit heating_widget(QWidget *parent = 0);
~heating_widget();
private slots:
void on_fullscreen_on_clicked();
private:
Ui::heating_widget *ui;
};
#endif // HEATING_WIDGET_H
and my heating.widget.cpp:
#include "heating_widget.h"
#include "ui_heating_widget.h"
#include "smarthome.h"
#include "iostream"
heating_widget::heating_widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::heating_widget)
{
ui->setupUi(this);
QObject::connect(ui->fullscreen_on, SIGNAL(clicked()), this , SLOT(SmartHome::setFullscreen()));
}
heating_widget::~heating_widget()
{
delete ui;
}
void heating_widget::on_fullscreen_on_clicked()
{
parentWidget()->setWindowState(Qt::WindowFullScreen);
std::cout<<"clicked"<<std::endl;
}
I would do it in the following way:
void heating_widget::on_fullscreen_on_clicked()
{
foreach(QWidget *widget, QApplication::topLevelWidgets()) {
if (auto mainWindow = qobject_cast<SmartHome *>(widget)) {
mainWindow->setWindowState(Qt::WindowFullScreen);
}
}
}
The idea is finding your main window among application top level widgets and change its state. This code can be called from anywhere in your application regardless of the windows hierarchy.

QT program crashes after connect()

I'm trying to write my new app, but it crashes every time I press a button on QDialog.
Here's my code :
mainwindow.h
#include <QMainWindow>
#include "creatlist.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
QDialog* creatList;
public slots:
void tableFull(){
...some code here...
}
private:
Ui::MainWindow *ui;
};
creatlist.h :
#include <QDialog>
#include "mainwindow.h"
namespace Ui {
class creatlist;
}
class MainWindow;
class creatlist : public QDialog
{
Q_OBJECT
public:
explicit creatlist(QWidget *parent = 0);
~creatlist();
MainWindow* mainwindow;
signals:
void updateList();
public slots:
void ready(){
///////////////////////////////////////////////////////////crash
connect(this,SIGNAL(updateList()),mainwindow,SLOT(tableFull()));
emit updateList();
}
private:
Ui::creatlist *ui;
};
If i try to send some signals my app crashes with a Segmentation Fault.
I did:
void creatlist::ready()
{
mainwindow = new MainWindow(this);
emit mainwindow->linktableFull();
}
but if I try to do QTextBroser.append("hue hue"); in linktableFull(), QTextBrowser is always empty.
Your QTextBrowser always empty because you create new mainwindow object in every ready() function. You should create mainwindow object once and use same mainwindow object throughout code. You can create new mainwindow object in creatlist constructor.

QWidget inherit from custom QWidget

In Qt C++, is it possible to create a custom QWidget and then reuse this custom QWidget for all QWidget (that inherit all from the custom QWidget) of the project?
Maybe I have misunderstood the question, but you can just create your custom QWidget, then use it everywhere.
class derivedQWidget : public QWidget
{
Q_OBJECT
derivedQWidget();
virtual ~derivedQWidget();
}
class myWidget : public derivedQWidget
{
...
}
class myWidget2 : public derivedQWidget
{
...
}
If the question is: Can we reimplement QWidget?, no you can't.
i have solved in this mode:
the first class, Widget.h:
#ifndef WIDGET_H
#define WIDGET_H
#include <QPushButton>
#include <QMouseEvent>
#include <QWidget>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
virtual ~Widget();
QPushButton *getBtn() const;
void setBtn(QPushButton *value);
protected:
void mousePressEvent(QMouseEvent *evt);
void mouseMoveEvent(QMouseEvent *evt);
private:
Ui::Widget *ui;
QPushButton *btn;
QPoint oldPos;
};
and the second class widExt.h, that inherit from Widget:
#ifndef WIDEXT_H
#define WIDEXT_H
#include "widget.h"
namespace Ui {
class widExt;
}
class widExt : public Widget
{
public:
widExt();
private slots:
void on_dial_2_actionTriggered(int action);
private:
Ui::widExt *ui;
};
#endif // WIDEXT_H
with the relative widExt.cpp:
#include "widext.h"
#include "ui_widext.h"
widExt::widExt() : ui(new Ui::widExt)
{
ui->setupUi(this);
}
void widExt::on_dial_2_actionTriggered(int action)
{
}
in this mode, i inherit all from the first class and i can customize other classes independently.

Transmitting data between classes

My code is set up in this way: the main window with a QTableWidget, and a control panel created with a different class from the MainWindow. In the control panel control there is a QListWidget where I want to load the titles of the header from the table, but being QTableWidget private, how can I pass data between the two classes?
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include "QControlPanel.h"
#include <QMainWindow>
#include <QWidget>
#include <QtGui>
#include <QTableWidget>
namespace Ui {
class MainWindow;
class GenerateXML;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
private:
Ui::MainWindow *ui;
QTableWidget* m_pTableWidget;
QControlPanel* preferences;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
this->setWindowIcon(QIcon("DrawItem.ico"));
this->setWindowTitle("DrawItem");
resize(890, 475);
m_pTableWidget = new QTableWidget(this);
m_TableHeader<<"A"<<"B"<<"C"<<"D"<<"E";
m_pTableWidget->setHorizontalHeaderLabels(m_TableHeader);
m_pTableWidget->resizeColumnsToContents();
m_pTableWidget->verticalHeader()->setVisible(false);
m_pTableWidget->move(30, 75);
m_pTableWidget->resize(410, 151);
preferences = new QControlPanel(this);
}
QControlPanel.h
#include <QDialog>
#include <QTableWidget>
#ifndef QCONTROLPANEL_H
#define QCONTROLPANEL_H
class QControlPanel : public QDialog
{
Q_OBJECT
public:
explicit QControlPanel(QWidget *parent = 0);
~QControlPanel();
public slots:
void customHeader(QTableWidget *table);
private:
QListWidget *headerlist;
private slots:
};
#endif
QControlPanel.cpp
#include "QControlPanel.h"
QControlPanel::QControlPanel(QWidget *parent) :
QDialog(parent)
{
headerlist = new QListWidget(inputHeader);
headerlist->setGeometry(140, 15, 140, 130);
}
You can have a signal in the QControlPanel class which retrieves the headers. So QControlPanel can be like :
class QControlPanel : public QDialog
{
Q_OBJECT
public:
explicit QControlPanel(QWidget *parent = 0);
~QControlPanel();
signals:
QStringList getHeaders();
private:
QListWidget *headerlist;
};
And connect the getHeaders() signal to a slot of the class which contains the list of columns to retrieve them. This should be done in the constructor of the MainWindow :
connect(preferences,SIGNAL(getHeaders()),this,SLOT(getTableHeader()));
getTableHeader() is a slot in MainWindow which return the header columns :
QStringList getTableHeader()
{
return m_TableHeader;
}
Now you can access the headers in QControlPanel :
QControlPanel::QControlPanel(QWidget *parent) :
QDialog(parent)
{
headerlist = new QListWidget();
headerlist->insertItems(0, getHeaders());
}

QString::toInt() fails when converting a QString from another class

I want to convert a QString from another class toInt, but it seams to fail because the result is 0 while it should be 5 and the rest of the code works.
Because I am new to C/Qt and find classes a bit confusing, I think I have done something wrong with my classes.
widget.cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_pushButton_clicked()
{
MyClass tmp;
int myNumber = tmp.m_replyStr.toInt();
ui->lcdNumber->display(myNumber);
}
MyClass::MyClass()
{
m_manager = new QNetworkAccessManager(this);
connect( m_manager, SIGNAL(finished(QNetworkReply*)),
this, SLOT(replyFinished(QNetworkReply*)));
}
void MyClass::fetch()
{
m_manager->get(QNetworkRequest(QUrl("http://example.tld/test.html"))); // html contains only the number 5
}
void MyClass::replyFinished(QNetworkReply* pReply)
{
QByteArray data=pReply->readAll();
QString str(data);
m_replyStr = str;
}
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QObject>
#include <QNetworkAccessManager>
#include <QUrl>
#include <QNetworkRequest>
#include <QNetworkReply>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private slots:
void on_pushButton_clicked();
private:
Ui::Widget *ui;
};
class MyClass : public QObject
{
Q_OBJECT
public:
MyClass();
void fetch();
QString m_replyStr;
public slots:
void replyFinished(QNetworkReply*);
private:
QNetworkAccessManager* m_manager;
};
#endif // WIDGET_H
You're accessing m_replyStr of a newly initialised instance tmp, which doesn't set anything into its m_replyStr. So it has the default-initialised value of "empty string."
EDIT
Based on your follow-up question, perhaps you were looking for something like this?
class MyClass;
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(MyClass &myClassInstance, QWidget *parent = 0);
~Widget();
private slots:
void on_pushButton_clicked();
private:
Ui::Widget *ui;
MyClass *myClass;
};
Widget::Widget(MyClass &myClassInstance, QWidget *parent = 0)
: QWidget(parent)
, ui(new Ui::Widget)
, myClass(&myClassInstance)
{
ui->setupUi(this);
}
void Widget::on_pushButton_clicked()
{
int myNumber = myClass->m_replyStr.toInt();
ui->lcdNumber->display(myNumber);
}