Qt QWidget::minimumSizeHint delay - c++

I want to write dialog with "spoiler". When I show/hide some elements, the form must resize to match her content. But I noticed that minimumSizeHint works with some delay. For example I wrote following dialog code:
dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
class Dialog : public QDialog {
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
private:
QPushButton *button1, *button2, *button3;
void adjust();
};
#endif // DIALOG_H
dialog.cpp
#include <QVBoxLayout>
#include <QPushButton>
#include <QTimer>
#include "dialog.h"
Dialog::Dialog(QWidget *parent) :
QDialog(parent) {
QVBoxLayout *layout = new QVBoxLayout;
button1 = new QPushButton("bad method", this);
button2 = new QPushButton("working method", this);
button3 = new QPushButton("indent", this);
layout->addWidget(button1);
layout->addWidget(button2);
layout->addWidget(button3);
setLayout(layout);
connect(button1, &QPushButton::pressed, [=]() {
button3->setVisible(!button3->isVisible());
adjust();
});
connect(button2, &QPushButton::pressed, [=]() {
button3->setVisible(!button3->isVisible());
QTimer *timer = new QTimer(this);
connect(timer, &QTimer::timeout, [=](){ adjust(); });
timer->setSingleShot(true);
timer->start(0);
});
}
void Dialog::adjust() {
resize(width(), minimumSizeHint().height());
}
When pressed button1, the form isn't match her content tightly, but when pressed button2, the form tightly match her content.
How to achieve results as pressing button2 without timers and other workarounds? Why button1 isn't work?

Consider that the minimum size is not computed until some events are processed in the event loop. If you don't want to use a timer, one workaround is to process the event loop for some iterations after the button is hidden or made visible, and then adjust size.
It's like :
button3->setVisible(!button3->isVisible());
for(int i=0;i<10;i++)
qApp->processEvents();
adjust();
A better solution is to call QLayout::activate() before resizing:
button3->setVisible(!button3->isVisible());
layout->activate();
adjust();
activate() forces your layout to recalculate the size hint.

I tried the QLayout::activate() solution, but it doesn't work for me.
QLayout::activate() returns false.
This is my code: this is a QMainWindow.
ui.groupBox->setVisible(!ui.groupBox->isVisible());
qDebug() << this->layout()->activate();
qDebug() << this->minimumSizeHint();
this->resize(this->minimumSizeHint());
any ideas why it's not working?
My current workaround is:
QTimer::singleShot(10, this, SLOT(on_resizeMin()));
but i noticed 10ms may not be enough on a slow system. Nasty workaround.

Related

Qt/C++ How can I disconnect a QProgressDialog::canceled signal to its QProgressDialog::cancel slot?

I have a QProgressDialog and I would like to override its cancel() slot to change its behavior.
Instead of closing the dialog, I would like to do some other operations and then close the dialog after a QThread to finish before closing the dialog.
I tried to disconnect the canceled/cancel signal/slot couples and the reconnect with the new behavior but it does not seem to change much.
As as soon as I click on the cancel button, the progress dialog gets closed first and then my lambda get executed anyway.
Qobject::disconnect(m_progressdialog, &QProgressDialog::canceled, m_progressdialog, &QProgressDialog::cancel);
Qobject::connect(m_progressdialog, &QProgressDialog::canceled, [](){
// continue displaying the dialog as an occupation bar
m_progressdialog->setValue(0);
// do some other things
// a lot of code
// ...
// only later close the dialog
m_progressdialog->close();
});
Is there a way to do this correctly?
I don't know your whole code, but according to the documentation, the idea is more or less the same you're saying: a slot to connect the signal QProgressDialog::canceled().
The following code is just an example but it's working. In this case, instead of using the own Qt property wasCanceled, it is used a boolean to control when to stop and cancel the QProgressDialog.
dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
class QProgressDialog;
QT_BEGIN_NAMESPACE
namespace Ui { class Dialog; }
QT_END_NAMESPACE
class Dialog : public QDialog
{
Q_OBJECT
public:
Dialog(QWidget *parent = nullptr);
~Dialog();
private slots:
void on_pushButton_clicked();
void my_custom_cancel();
private:
Ui::Dialog *ui;
QProgressDialog *progress;
int numTasks = 100000;
bool canceled = false;
};
#endif // DIALOG_H
dialog.cpp
#include "dialog.h"
#include "ui_dialog.h"
#include <QProgressDialog>
#include <QThread>
#include <QDebug>
Dialog::Dialog(QWidget *parent)
: QDialog(parent)
, ui(new Ui::Dialog)
{
progress = new QProgressDialog("Task in progress...", "Cancel", 0, numTasks);
connect(progress, SIGNAL(canceled()), this, SLOT(my_custom_cancel()));
ui->setupUi(this);
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::on_pushButton_clicked()
{
progress->open();
for (int i = 0; i < numTasks; i++) {
progress->setValue(i);
QThread::usleep(100);
if (canceled)
break;
}
progress->setValue(numTasks);
}
void Dialog::my_custom_cancel()
{
qDebug() << "do something";
canceled = true;
}

Qt: SIGNAL and SLOT don't work in a custom QDialog window

I have an application in which I want to prompt a dialog window for further (advanced) editing of apps content. It has to be a modal window, so that's why it can't be a QMainWindow. I just can't seem to make connect() macro work in QDialogwindow.
Previously, I got around this by using the response of the QDialog and a getter function in my dialog class:
A screenshot of a simple query
Dialog_CreateNew mDialog(this);
mDialog.setModal(true);
if (mDialog.exec() == QDialog::Accepted)
{
QString username, password;
mDialog.getData(&username, &password);
}
I used built-in SLOTs of QDialog library accept() and reject():
connect(_cancel, SIGNAL(clicked()), this, SLOT(reject()));
connect(_createNew, SIGNAL(clicked()), this, SLOT(accept()));
But now when I have to create my own slots in the dialog window, idk how to make it work. It will be a complex window, and just accept() and reject() won't do it.
One more thing: when I add Q_OBJECT macro in the dialog class, I get an error:
error: undefined reference to `vtable for Dialog_CreateNew'
These built-in SLOTs accept() and reject() work without Q_OBJECT.
I've seen this work in the Qt Designer, therefore it is possible. I don't want to use the Designer, everything ought to be done in coding.
That's my research and the things I tried.
My question is: How to make a signal-slot mechanism work in a modal child dialog window?
"dialog_createNew.h":
#ifndef DIALOG_CREATENEW_H
#define DIALOG_CREATENEW_H
#include <QtCore>
#include <QDialog>
#include <QIcon>
#include <QWidget>
#include <QLabel>
#include <QLineEdit>
#include <QGridLayout>
#include <QPushButton>
#include <QMessageBox>
#include <QStatusBar>
class Dialog_CreateNew : public QDialog
{
Q_OBJECT
public:
Dialog_CreateNew(QWidget* parent);
void getData(QString* username, QString* password);
void setErrorTip(QString errorTip);
virtual ~Dialog_CreateNew();
private:
QWidget* _parent;
QLabel* _lInstruction;
QLabel* _lUsername;
QLabel* _lPassword;
QLineEdit* _edUsername;
QLineEdit* _edPassword;
QPushButton* _createNew;
QPushButton* _cancel;
QLabel* _lErrorTip;
QString* _errorTip;
QGridLayout* _layout;
void createConnects();
private slots:
void onTextChanged_connect(QString);
};
#endif // DIALOG_CREATENEW_H
"dialog_createNew.cpp":
#include "dialog_createnew.h"
Dialog_CreateNew::Dialog_CreateNew(QWidget* parent)
{
_parent = parent;
this->setWindowTitle("Create New Bot");
this->setWindowIcon(QIcon(":/icons/createNew.svg"));
int nHeight = 150;
int nWidth = 360;
this->setGeometry(parent->x() + parent->width()/2 - nWidth/2,
parent->y() + parent->height()/2 - nHeight/2,
nWidth, nHeight);
this->setFixedSize(QSize(nWidth, nHeight));
_lInstruction = new QLabel(this);
_lInstruction->setText("Enter Your Instagram credentials:");
_lUsername = new QLabel(this);
_lUsername->setText("Username:");
_lPassword = new QLabel(this);
_lPassword->setText("Password:");
_edUsername = new QLineEdit(this);
_edUsername->setPlaceholderText("classybalkan");
_edPassword = new QLineEdit(this);
_edPassword->setEchoMode(QLineEdit::Password);
_edPassword->setPlaceholderText("•••••••••••");
_createNew = new QPushButton(this);
_createNew->setText("Create New Bot");
_cancel = new QPushButton(this);
_cancel->setText("Cancel");
_errorTip = new QString("");
_lErrorTip = new QLabel(this);
_layout = new QGridLayout(this);
_layout->addWidget(_lInstruction, 0, 0, 1, 2);
_layout->addWidget(_lUsername, 1, 0);
_layout->addWidget(_edUsername, 1, 1);
_layout->addWidget(_lPassword, 2, 0);
_layout->addWidget(_edPassword, 2, 1);
_layout->addWidget(_lErrorTip, 3, 1);
_layout->addWidget(_cancel, 4, 0);
_layout->addWidget(_createNew, 4, 1);
this->setLayout(_layout);
createConnects();
}
void Dialog_CreateNew::createConnects()
{
connect(_cancel,
SIGNAL(clicked()),
this,
SLOT(reject()));
connect(_edPassword,
&QLineEdit::textChanged,
this,
&Dialog_CreateNew::onTextChanged_connect);
}
void Dialog_CreateNew::getData(QString* username, QString* password)
{
*username = _edUsername->text();
*password = _edPassword->text();
}
void Dialog_CreateNew::setErrorTip(QString errorTip)
{
*_errorTip = errorTip;
_lErrorTip->setText("<center><font color=""red"">"
+ *_errorTip +
"</font><center>");
}
void Dialog_CreateNew::onTextChanged_connect()
{
QString text = _edPassword->text();
if (text == "")
{
disconnect(_createNew,
&QPushButton::clicked,
this,
&QDialog::accept);
_lErrorTip->setText("Invalid Password");
}
else
{
connect(_createNew,
&QPushButton::clicked,
this,
&QDialog::accept);
}
}
Dialog_CreateNew::~Dialog_CreateNew()
{
}
Solution:
Changing old SIGNAL/SLOT notation with the new function binding:
connect(_edPassword, &QLineEdit::textChanged, this, &Dialog_CreateNew::onTextChanged_connect);
This allowed me to use my own slots, and did the fix.
Three things you should do to fix this:
Split your work to header (.h file) and source (.cpp file)
Define the destructor of your dialog as virtual, e.g.:
virtual ~MyDialog()
{
}
Run qmake again
One or more of these will fix it for you
PS: Please, oh, Please... stop using the outdated SIGNAL() and SLOT(), and start using the new format that uses function binding, like:
connect(_cancel, &QPushButton::clicked, this, &QDialog::reject);
It's faster, more efficient, doesn't use strings, and once compiled, works. Unlike the old format that can fail at runtime.

QMenu not execing at correct position first time

I have this very strange issue regarding a QMenu and its position when execing.
Here is the code for my subclassed QMenu:
DockItemContextMenu::DockItemContextMenu(QWidget *parent) : QMenu(parent){
style = qApp->style();
QPointer<QAction> restoreAction = new QAction(QIcon(style->standardIcon(QStyle::SP_TitleBarMaxButton)), "Restore", this);
QPointer<QAction> minimizeAction = new QAction(style->standardIcon(QStyle::SP_TitleBarMinButton), "Minimize", this);
QPointer<QAction> maximizeAction = new QAction(style->standardIcon(QStyle::SP_TitleBarMaxButton), "Maximize", this);
QPointer<QAction> stayOnTopAction = new QAction("Stay On Top", this);
stayOnTopAction->setCheckable(true);
QPointer<QAction> closeAction = new QAction(style->standardIcon(QStyle::SP_TitleBarCloseButton), "Close", this);
this->addActions({restoreAction, minimizeAction, maximizeAction, stayOnTopAction, closeAction});
connect(restoreAction, &QAction::triggered, parent, [this](){ emit restoreTriggered();}, Qt::QueuedConnection);
connect(minimizeAction, &QAction::triggered, parent, [this](){ emit minimizeTriggered();}, Qt::QueuedConnection);
connect(maximizeAction, &QAction::triggered, parent, [this](){ emit maximizeTriggered();}, Qt::QueuedConnection);
connect(stayOnTopAction, &QAction::triggered, parent, [this](){ emit stayOnTopTriggered();}, Qt::QueuedConnection);
connect(closeAction, &QAction::triggered, parent, [this](){ emit closeTriggered();}, Qt::QueuedConnection);
}
Okay, so essentially I have another widget who holds an instance of this DockItemContextMenu as a field. In this owning class, called Titlebar, I made it such that doing a right click will emit the customContextMenuRequested(QPoint) signal.
TitleBar::TitleBar(QString title, QWidget *parent){
...
this->setContextMenuPolicy(Qt::CustomContextMenu);
contextMenu = new DockItemContextMenu(this);
connect(this, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(showContextMenu(QPoint)), Qt::QueuedConnection);
...
}
After this, this widget is essentially inserted into a QGraphicsScene and is converted implicitly into a QGraphicsItem. When I do the FIRST right click event on my Titlebar it will not exec at the correct screen position if I dragged the MainWindow of the entire QApplication anywhere other than its starting position on screen. In addition to being in a QGraphicsScene, this scene itself is always stored in a QSplitter. Now I would understand if this always had some sort of issue, but it turns out, every time I call the slot for that signal, ONLY the first time will it exec in the incorrect position in the QGraphicsScene. No matter how I manipulate the size of the Titlebar widget itself, move commands or maximize commands to the MainWindow, or even edit the splitter size for the QGraphicsView that affects the size of the QGraphicsScene, it will always be in the correct position afterwards. here is the function for execing:
void TitleBar::showContextMenu(QPoint point){
qDebug() << point;
contextMenu->exec(point);
emit _parent->focusChangedIn();
}
I printed the point at which it is calling the exec. The strangest part is that both times I right click in the same location, it will print the SAME value for the slot's positional parameter both the first exec and second exec, but be in the correct location every time other than the first. Did I forget to set some other flag when I added the context menu to the Titlebar class? Does it have anything to do with setting the QMenu's parent to the Titlebar? I'm just dumbfounded how the same QPoint could exec at two different screen locations given the same value. Does anybody have a clue what may or may not be happening on the first call to the Titlebar's slot for execing the QMenu?
EDIT: The issue stemmed from doing this line of code in the Titlebar constructor:
contextMenu = new DockItemContextMenu(this);
Changing it to:
contextMenu = new DockItemContextMenu;
fixed the issue. Does anyone know why, or is this possibly a bug? I rather not accept this as an answer because it does not explain why it happened in the first place.
Here is a minimal example with the same effect.
MainWindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QWidget>
#include <QGraphicsView>
#include <QSplitter>
#include <QHBoxLayout>
#include <QGraphicsScene>
#include <QPointer>
#include <QTreeWidget>
#include "titlebar.h"
class MainWindow : public QMainWindow{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
private:
};
#endif // MAINWINDOW_H
MainWindow.cpp:
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent){
QPointer<QWidget> widgetArea = new QWidget;
QPointer<QHBoxLayout> hLayout = new QHBoxLayout;
widgetArea->setLayout(hLayout);
QPointer<QSplitter> splitter = new QSplitter;
hLayout->addWidget(splitter);
QPointer<QTreeView> tree = new QTreeView;
splitter->addWidget(tree);
QPointer<QGraphicsView> view = new QGraphicsView;
splitter->addWidget(view);
splitter->setStretchFactor(0, 1);
splitter->setStretchFactor(1, 4);
QPointer<QGraphicsScene> scene = new QGraphicsScene;
view->setScene(scene);
QPointer<Titlebar> blue = new Titlebar;
blue->setObjectName("blue");
blue->setStyleSheet(QString("#blue{background-color: rgb(0,0,255)}"));
blue->resize(250,250);
scene->addWidget(blue);
this->setCentralWidget(widgetArea);
this->resize(1000,750);
}
MainWindow::~MainWindow(){
}
Titlebar.h:
#ifndef TITLEBAR_H
#define TITLEBAR_H
#include <QMenu>
#include <QWidget>
#include <QPointer>
#include <QDebug>
#include <QMouseEvent>
class Titlebar : public QWidget{
Q_OBJECT
public:
explicit Titlebar(QWidget *parent = nullptr);
QPointer<QMenu> menu;
QPoint currentPos;
protected slots:
void mousePressEvent(QMouseEvent* event);
void mouseMoveEvent(QMouseEvent* event);
void showContextMenu(QPoint point);
};
#endif // TITLEBAR_H
Titlebar.cpp:
#include "titlebar.h"
Titlebar::Titlebar(QWidget *parent) : QWidget(parent){
setContextMenuPolicy(Qt::CustomContextMenu);
connect(this, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(showContextMenu(QPoint)), Qt::QueuedConnection);
menu = new QMenu(this);
menu->addAction("Test");
}
void Titlebar::showContextMenu(QPoint point){
qDebug() << point;
menu->exec(mapToGlobal(point));
}
void Titlebar::mouseMoveEvent(QMouseEvent *event){
if (event->buttons() && Qt::LeftButton){
QPoint diff = event->pos() - currentPos;
move(pos() + diff);
}
}
void Titlebar::mousePressEvent(QMouseEvent * event){
currentPos = event->pos();
}
main.cpp:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
So this runs and reproduces the error accordingly. If you change the line in Titlebar.cpp from
menu = new QMenu(this);
to:
menu = new QMenu;
Then it works correctly. ONLY the first right click to open the context menu will spawn in the incorrect location on screen. All subsequent right clicks will now follow either the widget/window/splitter in any combination. I don't get it, can someone tell me if this is actually a bug or not.
You need to add one line of code because your using a QGraphicsProxyWidget which is part of a QGraphicsScene. The scene is represented by a QGraphicsView which inherits QAbstractScrollArea. This causes the context menu to be shown via the viewport and not the widget itself. Therefore adding this one line of code will override the title bar to not be embedded in the scene when it's parent was already embedded in the scene. Effectively making it reference the widget again and not the viewport.
In the MainWindow.cpp right after line 26 add
blue->setWindowFlags(Qt::BypassGraphicsProxyWidget);

Creating sequenced tabs in Qt

A program has the main window, the menu bar, the menu item (QAction in Qt), the tab widget, the text edit. I try to receive the sequenced numeration in the tabs when I press on the menu item (New Tab).
When I press on the New Tab then tab 1, tab 2, tab 3, tab 4 and so on must appear.
The suggested approximate code is here:
MainWindow::MainWindow(QWidget* parent):QMainWindow(parent)
{
QMenuBar* menuBar = new QMenuBar(this);
setMenuBar(menuBar);
QMenu* fileMenu = new QMenu("&File", this);
menuBar->addMenu(fileMenu);
QAction* newTabAction = new QAction("&New Tab", this);
fileMenu->addAction(newTabAction);
connect(newTabAction, SIGNAL(triggered()), this, SLOT(newTabActionHandler()));
QTabWidget* tabWidget = new QTabWidget(this);
QList<QWidget*> widgetList;
widgetList.append(new QWidget(this));
tabWidget->addTab(widgetList[0], "Tab 0");
tabWidget->setMovable(true);
tabWidget->setTabsClosable(true);
QList<QTextEdit*> textEditList;
textEditList.append(new QTextEdit(this));
QVBoxLayout* vBoxLayout = new QVBoxLayout();
widgetList[0]->setLayout(vBoxLayout);
vBoxLayout->addWidget(textEditList[0]);
setCentralWidget(tabWidget);
}
void MainWindow::newTabActionHandler()
{
widgetList.append(new QWidget(this));
tabWidget->addTab(widgetList[widgetList.size()-1], ????);
textEditList.append(new QTextEdit(this));
QVBoxLayout* vBoxLayout = new QVBoxLayout();
widgetList[widgetList.size()-1]->setLayout(vBoxLayout);
vBoxLayout->addWidget(textEditList[textEditList.size()-1]);
}
Please, put the correct code into the line where question signs take place to be (in the newTabActionHandler() method body).
tabWidget->addTab(widgetList[widgetList.size()-1], ????);
Thank You!
You have to order your code, in this case you only need to use the size of the list. but I have given the freedom to correct your code, for example widgetList and textEditList are local variables so you can not access from the slot so it is appropriate that they are members of the class.
Another recommendation is to order your code, the more readable your code is, so you can create widget and textedit and make your links without using your containers.
mainwindow.cpp
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QTextEdit>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void newTabActionHandler();
private:
QList<QWidget*> widgetList;
QList<QTextEdit*> textEditList;
QTabWidget* tabWidget;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include <QMenu>
#include <QMenuBar>
#include <QTextEdit>
#include <QVBoxLayout>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
QMenuBar *menuBar = new QMenuBar(this);
setMenuBar(menuBar);
QMenu* fileMenu = new QMenu("&File", this);
menuBar->addMenu(fileMenu);
QAction *newTabAction = new QAction("&New Tab", this);
fileMenu->addAction(newTabAction);
connect(newTabAction, &QAction::triggered, this, &MainWindow::newTabActionHandler);
tabWidget = new QTabWidget(this);
tabWidget->setMovable(true);
tabWidget->setTabsClosable(true);
newTabActionHandler();
setCentralWidget(tabWidget);
}
void MainWindow::newTabActionHandler()
{
QWidget *widget = new QWidget;
widgetList << widget;
tabWidget->addTab(widget, QString("Tab %1").arg(widgetList.size()-1));
QTextEdit *textEdit = new QTextEdit;
textEditList << textEdit;
QVBoxLayout* vBoxLayout = new QVBoxLayout(widget);
vBoxLayout->addWidget(textEdit);
}
MainWindow::~MainWindow()
{
}
You can find the complete example in the following link
Replace ???? by QString("Tab %1").arg(widgetList.size()-1)

Qt: Force a repaint to update text from fast button clicks?

I have some very simple code that displays a QPushButton which, when clicked, updates a spinbox with a random number 1 - 100. The problem is I can click the button many times in quick succession and only see one or two updates in the spinbox.
How can I repaint the spinbox for each click on the QPushButton? I've verified that I am firing and catching multiple click signals, but Qt doesn't repaint most of them.
So far I've tried calling repaint(), repaint() on all parent widgets, sendPostedEvents(), and processEvents().
#include <QtWidgets/QWidget>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QSpinBox>
#include <QtWidgets/QLayout>
#include <random>
#include <ctime>
class QtBtnEx : public QWidget
{
Q_OBJECT
public:
QtBtnEx(QWidget *parent = 0);
QPushButton* btn;
QSpinBox* spin;
public slots:
void onClicked();
};
QtBtnEx::QtBtnEx(QWidget *parent)
: QWidget(parent)
{
btn = new QPushButton("button");
spin = new QSpinBox();
btn->setFixedSize(90, 30);
spin->setFixedSize(90, 30);
this->setLayout(new QVBoxLayout());
this->layout()->setAlignment(Qt::AlignCenter);
this->layout()->addWidget(btn);
this->layout()->addWidget(spin);
connect(btn, &QPushButton::clicked, this, &QtBtnEx::onClicked);
}
//Fires for every button click but does not paint for every click
void QtBtnEx::onClicked()
{
srand(time(nullptr));
spin->setValue(rand() % 100);
}
Found my problem; I had the call to srand(time(nullptr)) in the slot code. It was responsible for the delays I was seeing. Pulled it up top and the spinbox refreshes immediately.
#include <QtWidgets/QWidget>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QSpinBox>
#include <QtWidgets/QLayout>
#include <random>
#include <ctime>
class QtBtnEx : public QWidget
{
Q_OBJECT
public:
QtBtnEx(QWidget *parent = 0);
QPushButton* btn;
QSpinBox* spin;
public slots:
void onClicked();
};
QtBtnEx::QtBtnEx(QWidget *parent)
: QWidget(parent)
{
srand(time(nullptr));
btn = new QPushButton("button");
spin = new QSpinBox();
btn->setFixedSize(90, 30);
spin->setFixedSize(90, 30);
this->setLayout(new QVBoxLayout());
this->layout()->setAlignment(Qt::AlignCenter);
this->layout()->addWidget(btn);
this->layout()->addWidget(spin);
connect(btn, &QPushButton::clicked, this, &QtBtnEx::onClicked);
}
void QtBtnEx::onClicked()
{
spin->setValue(rand() % 100);
}