How to get a signal when the widget size changes - c++

I need to perform an operation when the size of a widget (MyForm) changes.
Is there a signal which is emitted when the size of a widget changes? (I could not find it in the documentation).
I need to do something like this:
#include "myform.h"
#include "ui_myform.h"
MyForm::MyForm(QWidget *parent) :
QWidget(parent),
ui(new Ui::MyForm)
{
ui->setupUi(this);
connect(this, SIGNAL(sizeChanged()), this, SLOT(refresh()));
}
MyForm::~MyForm()
{
delete ui;
}
void MyForm::refresh()
{
ui->label->setText( QString::number(this->width()) + ", " + QString::number(this->height()));
}
When the widget size changes, it 'calls' the refresh function which updates a label with the current width and height. Note: it is only an example which can be easily reproduced.
Sure I can use a QTimer, for example:
#include "myform.h"
#include "ui_myform.h"
MyForm::MyForm(QWidget *parent) :
QWidget(parent),
ui(new Ui::MyForm)
{
ui->setupUi(this);
timer_ = new QTimer(this);
connect(timer_, SIGNAL(timeout()), this, SLOT(refresh()));
timer_->start(100);
}
MyForm::~MyForm()
{
delete ui;
}
void MyForm::refresh()
{
ui->label->setText( QString::number(this->width()) + ", " + QString::number(this->height()));
}
But I don't think it is the best solution.
Note: I'm using Qt 5.3.

It is not necessary to create a signal, you just have to overwrite resizeEvent() and call refresh from that method:
*.h
protected:
void resizeEvent(QResizeEvent *event);
*.cpp
void MyForm::resizeEvent(QResizeEvent *event)
{
refresh();
QWidget::resizeEvent(event);
}

Related

How to identify the pressed button in Qt C++?

I have 4 buttons on my main window. Each button opens its own window with its own data. How to identify the pressed button to open right window? For example: I press sales button and it opens a window that shows information about ticket sales.
Mainwindow ui
Here is my code from mainwindow h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <sales.h>
#include <theatres.h>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void button_pressed();
private:
Ui::MainWindow *ui;
sales *s;
theatres *t;
};
#endif // MAINWINDOW_H
And here is my code from mainwindow cpp:
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include "build/sqlite/sqlite3.h"
#include <QtSql/QSqlDatabase>
#include <QTableView>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect((*ui).pushButton,SIGNAL(released()), this, SLOT(button_pressed()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::button_pressed()
{
s = new sales(this);
s -> show();
}
As Andy Newman already answered
the shortest solution is a lambda function
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPushButton>
#include <QHBoxLayout>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QHBoxLayout *h_layout = new QHBoxLayout;
centralWidget()->setLayout(h_layout);
for(int c =1; c <= 10; c++)
{
QPushButton *button = new QPushButton(this); // create button
button->setText(QString::number(c)); // set button id
h_layout->addWidget(button); // add a button to the form
// lambda magic
/* connecting a button signal to a lambda function that captures a pointer to a
button and invokes an arbitrary type function. */
connect(button, &QPushButton::clicked, [this, button]() {
pressedButton(button->text());
});
}
}
void MainWindow::pressedButton(const QString &id_button)
{
qDebug("Pressed button: %ls", id_button.utf16());
}
MainWindow::~MainWindow()
{
delete ui;
}
#include "widget.h"
#include "./ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
connect(ui->btn_0,&QPushButton::clicked,this,&Widget::SlotButtonClicked);
connect(ui->btn_1,&QPushButton::clicked,this,&Widget::SlotButtonClicked);
connect(ui->btn_2,&QPushButton::clicked,this,&Widget::SlotButtonClicked);
connect(ui->btn_3,&QPushButton::clicked,this,&Widget::SlotButtonClicked);
}
Widget::~Widget()
{
delete ui;
}
void Widget::SlotButtonClicked()
{
auto sender = this->sender();
if ( sender == ui->btn_0 ) {
// Click btn_0 to open widget0
} else if ( sender == ui->btn_1 ) {
// Click btn_1 to open widget1
} else if ( sender == ui->btn_2 ) {
// Click btn_2 to open widget2
} else if ( sender == ui->btn_3 ) {
// Click btn_3 to open widget3
}
}
If you can use Qt Designer, the best way to do this is to click with button right on the QPushButton (On .ui file in Qt Designer) and click to "Go to Slot", this will create a private slot to this button! In the header file will create the definition, like this:
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
void on_pushButton_3_clicked();
void on_pushButton_4_clicked();
And in the source file (.cpp) will create the "function" clicked pushButton:
void MainWindow::on_pushButton_clicked()
{
}
void MainWindow::on_pushButton_2_clicked()
{
}
void MainWindow::on_pushButton_3_clicked()
{
}
void MainWindow::on_pushButton_4_clicked()
{
}
Inside of the "function" in .cpp, you put the task that you want this button to do, in this case, to open a new window!
When you click "go to slot" in another button, will create another private slot with the respective number (If is the second QPushButton that you create, the private slot will be called by pushButton_2).
The usual way to do this would be to connect the 4 different buttons to 4 different slots. It looks like you are using QtDesigner so that shouldn't be an issue.
If you were generating an array of buttons at run time you'd run into problems and would need a different solution. You could try something like this to pass an array index to the function, for example:
connect(button[x], &QPushButton::clicked, this, [this, x]() { button_pressed(x); });
Or you could do it the Qt way, which would be to call ::setProperty to store data in the button, and then retrieve it from the event, but it's so esoteric that I can't actually remember how to do that...

Qt signal and slots do not work if called from QRunnable or another thread

I am trying to learn how to use Multi-threading in Qt and put it to use in QtWidget applications. So I have setup this simple test case.
MainWindow
This form has some buttons and each button will execute another form (a Dialog for that matter).
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->btnShowCalibrationDialog, &QPushButton::clicked,
this, [&](){
CalibrationDialog dlg;
dlg.exec();
});
}
MainWindow::~MainWindow()
{
delete ui;
}
CalibrationDialog This dialog has a QPushButton and a QTextEdit. When the button is clicked I will run a QRunnable class (comes next!)
calibrationdialog.h (I have spared you the includes and guards!)
class CalibrationDialog : public QDialog
{
Q_OBJECT
public:
explicit CalibrationDialog(QWidget *parent = nullptr);
~CalibrationDialog();
public slots:
void onBeginWorkRequested();
void onReceiveData(int data);
private:
Ui::CalibrationDialog *ui;
};
calibrationdialog.cpp
#include "worker.h"
CalibrationDialog::CalibrationDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::CalibrationDialog)
{
ui->setupUi(this);
connect(ui->btnBegin, &QPushButton::clicked,
this, &CalibrationDialog::onBeginWorkRequested);
}
CalibrationDialog::~CalibrationDialog()
{
delete ui;
}
void CalibrationDialog::onBeginWorkRequested()
{
qDebug() << "CalibrationDialog::onBeginWorkRequested()" << "on" << QThread::currentThreadId();
Worker* worker = new Worker();
QThreadPool* pool = QThreadPool::globalInstance();
connect(worker, &Worker::reportProgress,
this, &CalibrationDialog::onReceiveData);
pool->start(worker);
pool->waitForDone();
}
void CalibrationDialog::onReceiveData(int data)
{
ui->teResults->append(QString::number(data));
ui->teResults->ensureCursorVisible();
}
Worker And this is some runnable...I want to report the progress so that is shows up in the textedit of the dialog in a reponsive manner!
worker.h
class Worker : public QObject, public QRunnable
{
Q_OBJECT
public:
explicit Worker(QObject *parent = nullptr);
void run() override;
private:
int mProgress = 0;
signals:
void reportProgress(int progress);
};
worker.cpp
Worker::Worker(QObject *parent) : QObject(parent)
{
}
void Worker::run()
{
qDebug() << "Worker is running #" << QThread::currentThreadId();
while(true) {
mProgress += 100;
emit reportProgress(mProgress);
QThread::msleep(500);
}
}
I see in the debugger that control goes in to the while loop...but the signal is not being handled by the dialog! I mean the textedit stays empty!
I tried to read documentationand search online...I came to the conclusion that my problem lies in the waste land of thread affinity....but I have no idea if that is the case and if that is, how to solve it. Please assist!
remove pool->waitForDone();, never use waitForX methods in a GUI since they are blocking preventing signals from doing their job.

Qt Drop event not firing

Drop event will not happen, although `setAcceptDrops' has been called. The following code is based on a widget project created with Qt 5.12.0. After adding in dropEvent() function the cpp file becomes
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug> // added
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setAcceptDrops(true); // added
}
MainWindow::~MainWindow()
{
delete ui;
}
// added; in .h it is in `protected:' section
void MainWindow::dropEvent(QDropEvent *event)
{
qDebug() << "dropEvent";
}
What am I missing? I have been struggling for a few days... Thanks in advance.
You have to overwrite the dragEnterEvent method that allow you to filter by the data type, by the source, by the type of action. In the following example, everything is accepted:
*.h
// ...
protected:
void dropEvent(QDropEvent *event) override;
void dragEnterEvent(QDragEnterEvent *event) override;
// ...
*.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setAcceptDrops(true); // added
}
// ...
void MainWindow::dropEvent(QDropEvent *event)
{
qDebug() << "dropEvent" << event;
}
void MainWindow::dragEnterEvent(QDragEnterEvent *event)
{
event->acceptProposedAction();
}
For more detail I recommend you read Drag and Drop.

mousePressEvent is working but mouseMoveEvent isn't

both events work properly on bare mainWindow but when I press inside the graphicsView ,placed inside the mainWindow, only mousePressEvent is responding.
could anybody clarify this issue?
UPD: Here is the code
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "mydialog.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
scene = new QGraphicsScene(this);
ui->graphicsView->setScene(scene);
pix = new QPixmap("/Users/mac/Pictures/wallpaper/Rocks.jpg");
scene->addPixmap(*pix);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::mousePressEvent(QMouseEvent *e)
{
sel_reg_beg_x = e->x();
sel_reg_beg_y = e->y();
qDebug() << "inside press";
}
void MainWindow::mouseMoveEvent(QMouseEvent *e)
{
qDebug() << "inside move";
sel_reg_end_x = e->x();
sel_reg_end_y = e->y();
this->update();
}
You have two options here:
Derive your own graphics view from the QGraphicsView and implement the mouse move event handler there.
Create the event filter and install it into your QGraphicsView's viewport (ui->graphicsView->viewport()->installEventFilter(...)). See the QObject::eventFilter() documentation.
And of course, you have to enable mouse tracking also for the QGraphicsView's viewport:
ui->graphicsView->viewport()->setMouseTracking(true);

Trigger different event on QTimer reset

I am building a little program that shows traffic light images using QTimer. So i setup my timer and everything works good. But I cant figure out, how can I get the Robot Lights to ->show() and ->hide() each time the timer interval is reached. I can have this all wrong, im still learning so please advise.
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include<QTimer>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void timer();
QTimer *mytimer;
private:
Ui::MainWindow *ui;
int timerValue;
private slots:
void showGreen();
void showYellow();
void showRed();
void on_startButton_clicked();
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->green->setVisible(false);
ui->yellow->setVisible(false);
ui->red->setVisible(false);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::timer(){
ui->green->setVisible(true);
mytimer = new QTimer(this);
connect(mytimer,SIGNAL(timeout()),this,SLOT(showYellow()));
mytimer->start(timerValue = (ui->spinBox->value())*1000);
}
void MainWindow::showYellow(){
ui->yellow->setVisible(true);
mytimer = new QTimer(this);
connect(mytimer,SIGNAL(timeout()),this,SLOT(showRed()));
mytimer->start(timerValue);
}
void MainWindow::showRed(){
ui->red->setVisible(true);
mytimer = new QTimer(this);
connect(mytimer,SIGNAL(timeout()),this,SLOT(showGreen));
mytimer->start(timerValue);
}
void MainWindow::showGreen(){
ui->green->setVisible(true);
mytimer = new QTimer(this);
connect(mytimer,SIGNAL(timeout()),this,SLOT(showYellow()));
mytimer->start(timerValue);
}
void MainWindow::on_startButton_clicked()
{
timer();
ui->startButton->setEnabled(false);
}
I Also disable the Start button when clicked so the timer cannot run twice.
So On the main Window UI I basicly Have a Spinbox that takes the input of the user which is the time that I pass that to Qtimer, and then I have the 3 images with lights red green yellow that has to show and hide in the intervals . So I created almost something like a manual loop. Qtimer starts and shows Green, then goes to ShowYellow Slot and then showRed slot, so now, its suppose to go to the Green slot and then to yellow, but it doesnt go to Green again.
Can someone tell me why not.
Read the comments:
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
...
mytimer = new QTimer(this); // create QTimer once
}
void MainWindow::timer() {
timerValue = ui->spinBox->value() * 1000;
showGreen(); // reuse showGreen()
}
void MainWindow::showYellow() {
// this is how toggling can be done
ui->yellow->setVisible(!ui->yellow->isVisible());
mytimer->disconnect(); // disconnect before making a new connection
connect(mytimer ,SIGNAL(timeout()), this, SLOT(showRed()));
mytimer->start(timerValue);
}
void MainWindow::showRed() {
ui->red->setVisible(!ui->red->isVisible());
mytimer->disconnect();
connect(mytimer ,SIGNAL(timeout()), this, SLOT(showGreen()));
mytimer->start(timerValue);
}
void MainWindow::showGreen() {
ui->green->setVisible(!ui->green->isVisible());
mytimer->disconnect();
connect(mytimer ,SIGNAL(timeout()), this, SLOT(showYellow()));
mytimer->start(timerValue);
}
Perhaps most simply:
void MainWindow::showHide(){
ui->green->setVisible(!ui->green->isVisible());
}
The show() and hide() member functions of QWidget are equivalent to setVisible(true) and setVisible(false), respectively.
A problem you may run into later, if you press the start button more than once, is that you spawn a new interval timer every time, leading to quicker blinking with every press of the button. If this is not what you want to happen, you'll have to control the generation of new timers.