Child window elements are blocked in Qt C++ - c++

I have three windows: MainWindow, SelectPreset, Preset.
The SelectPreset window is called from the MainWindow window.
And the Preset window is called from the SelectPreset window.
But I can't understand why the Preset window elements are blocked (At the same time, this happens for some reason in Debian, in other systems I did not notice this).
Here's how my files work:
mainwindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
.
.
.
SelectPreset select_preset;
select_preset.setModal(true);
select_preset.exec();
selectpreset.cpp
SelectPreset::SelectPreset(QWidget *parent) :
QDialog(parent),
ui_selectpreset(new Ui::SelectPreset)
{
ui_selectpreset->setupUi(this);
}
.
.
.
Preset preset;
preset.setModal(true);
preset.exec();
preset.cpp
Preset::Preset(QWidget *parent) :
QDialog(parent),
ui_preset(new Ui::Preset)
{
ui_preset->setupUi(this);
}

Preset preset(this);
preset.setModal(true);
preset.exec();

Related

How can I programmatically make a window fullscreen?

How can I programmatically make a window fullscreen?
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
scene = new QNewScene(this);
ui->graphicsView->setScene(scene);
// here is some code that will make the window fullscreen
}
For example:
QTimer::singleShot(0, this, SLOT(showFullScreen()));
method to make your window full screen:
QWidget::showFullScreen()
check it here: https://doc.qt.io/qt-5/qwidget.html#showFullScreen

Connecting to a same signal in two windows

Can you think of a reason why this second scenario not working?
I have signal connection in two windows , Settings and Patients
In Settings:
Settings::Settings( QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Settings)
{
ui->setupUi(this);
connect(&api, &tetra_grip_api::tetraGripEvent,this, &Settings::eventHandler);
...
}
api is global instance of a class.
And in Patients:
Patients::Patients( QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Patients)
{
ui->setupUi(this);
connect(&api, &tetra_grip_api::tetraGripEvent,this, &Patients::eventHandlerTwo);
}
Situation1 - Working
I construct both Settings and Patients window from Main
#include <QApplication>
tetra_grip_api api;
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
api.openSerialPort();
QObject::connect(api.serial, SIGNAL(readyRead()), &api, SLOT(readData()));
Settings w(nullptr); //---->this behaves right
Patients v(nullptr); //---- >this behaves right
v.show();
w.show();
return a.exec();
}
By working I mean , both the slots are being called , and QLabel set text accordingly
Situation 2 - Not working
I call Patients from Settings:
void Settings::on_pushButton_patients_clicked()
{
this->close();
stagetwo = new Patients(this);
stagetwo -> show();
}
where stagetwo is public
public:
Settings(QString,QWidget *parent = nullptr);
~Settings();
Patients *stagetwo;
Here Settings works just fine (slot being called) but Patients::eventhandlerTwo is not being called at all.
EDIT (Found the bug)
It's bit late to understand that I need to call a method in the API (battery_percentage) to emit signal tetraGripEvent ( bettery_percentage will contact "battery register" in the device and this will force API to emit signal)
basically I need to do this on both Settings and Patients
#include "settings.h"
#include "ui_settings.h"
Settings :: Settings (QWidget *parent) : QMainWindow(parent)
QMainWindow(parent)
, ui(new Ui::Settings )
{
ui->setupUi(this);
connect(&api, &tetra_grip_api::tetraGripEvent,this, &Settings::eventHandler);
...
tetra_grip_api::battery_percentage(); ------> calling this only emit the signal
}
and in Patients
#include "patients.h"
#include "ui_patients.h"
Patients::Patients(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Patients)
{
ui->setupUi(this);
connect(&api, &tetra_grip_api::tetraGripEvent,this, &Patients::eventHandlerTwo);
...
tetra_grip_api::battery_percentage();
}
As per Qt documentation for QWidget::close(), which QMainWindow is being inherited from:
The QApplication::lastWindowClosed() signal is emitted when the last
visible primary window (i.e. window with no parent) with the
Qt::WA_QuitOnClose attribute set is closed. By default this attribute
is set for all widgets except transient windows such as splash
screens, tool windows, and popup menus.
So, in the second scenario, when you set the parent of Patients to a Settings instance, you should expect an undefined behavior like this. Because by calling Settings::close() should delete Settings and quit the application when the call returns to the event loop. You can explicitly change it by :
Settings::Settings( QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Settings)
{
ui->setupUi(this);
this->setAttribute(Qt::WA_QuitOnClose, false);
connect(&api, &tetra_grip_api::tetraGripEvent,this, &Settings::eventHandler);
...
}
You can also reparent both windows to another window.

QVideoWidget doesn't resize well

I have a Qt application which simply captures from the default webcam and shows it on a QVideoWidget. In the ui, I have a simple MainWindow with a QGraphicsView inside a VerticalLayout:
ui design
My mainwindow.cpp=============================================
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
m_viewfinder = new QVideoWidget(ui->captureView);
m_camera = new QCamera(QCameraInfo::defaultCamera());
m_camera->setViewfinder(m_viewfinder);
m_camera->start();
}
MainWindow::~MainWindow()
{
m_camera->stop();
delete m_viewfinder;
delete m_camera;
delete ui;
}
When I execute this, I get the application running, but the video contents do not scale according to the mainwindow size. Examples:
When I start the application
Resizing mainwindow down
Resizing mainwindow up
Is there some way to make the video content resize well and fit the available area?
I have seen this answer: QVideoWidget: Video is cut off, but it doesn't offer any solution that works for me. When use the QGraphicsView-QGraphicsScene-QGraphicsVideoItem chain, I see nothing at all.
When you use the following instruction:
m_viewfinder = new QVideoWidget(ui->captureView);
You are setting as the parent of m_viewfinder to captureView, so the positions of m_viewfinder will be relative to captureView, but this does not indicate that it will be the same size as the parent.
One of the easiest ways to do this is to use a layout. Also, it is not necessary to create the QGraphicsWidget or the QVBoxLayout, so I recommend you to delete it and get the design as it was established by default:
and then we establish a layout that is placed in the centralWidget, and in this layout we add the QVideoWidget.
...
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
m_viewfinder = new QVideoWidget;
QVBoxLayout *lay = new QVBoxLayout(ui->centralWidget);
lay->addWidget(m_viewfinder);
m_camera = new QCamera(QCameraInfo::defaultCamera());
m_camera->setViewfinder(m_viewfinder);
m_camera->start();
}
...
In the following link you can find the complete example.

QGraphicsDropShadowEffect works only for one item on ui, although I used it everywhere

I'm using QGraphicsDropShadowEffect to make my GUI look pretier. Minimal working sample:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QGraphicsDropShadowEffect>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QGraphicsDropShadowEffect *g = new QGraphicsDropShadowEffect(this);
ui->pushButton->setGraphicsEffect(g);
ui->pushButton_2->setGraphicsEffect(g);
ui->pushButton_3->setGraphicsEffect(g);
}
MainWindow::~MainWindow()
{
delete ui;
}
As you see, I have 3 buttons and want to have a fancy shadow at the top of every button. Although I set graphics effect on every button it can be seen only on the last button, heres the image:
How can I improve it and what is the cause?
This works:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QGraphicsDropShadowEffect>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QGraphicsDropShadowEffect *g1 = new QGraphicsDropShadowEffect(this);
QGraphicsDropShadowEffect *g2 = new QGraphicsDropShadowEffect(this);
QGraphicsDropShadowEffect *g3 = new QGraphicsDropShadowEffect(this);
ui->pushButton->setGraphicsEffect(g1);
ui->pushButton_2->setGraphicsEffect(g2);
ui->pushButton_3->setGraphicsEffect(g3);
}
MainWindow::~MainWindow()
{
delete ui;
}
but seem not to be the best solution I can have.
It's the normal behavior of the function you are calling
see documentation
http://qt-project.org/doc/qt-4.8/qgraphicsitem.html#setGraphicsEffect
If effect is the installed on a different item, setGraphicsEffect()
will remove the effect from the item and install it on this item.

QDialog is blank with show command

If I create my QDialog and show it modal with exec() everything works fine, but I need this no modal!
With show() the Dialog is empty!
ProgramLoading *programLoading = new ProgramLoading();
programLoading->show();
// some code
programLoading->done(0);
Constructor
ProgramLoading::ProgramLoading(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);
setWindowFlags( Qt::CustomizeWindowHint ); // remove window border
}
Don't think something with Dialog code is wrong because it works with exec()!
Any hints? Thank you!
PS: I'm using QT plugin for VisualStudio 2008
mw (Default with Window Decoration):
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
sd = new SplashDialog();
sd->show();
QTimer::singleShot(10000,this,SLOT(closeSplashDialog()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::closeSplashDialog()
{
sd->close();
delete sd;
}
splash (UI having Label and Button):
SplashDialog::SplashDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::SplashDialog)
{
ui->setupUi(this);
setWindowFlags( Qt::CustomizeWindowHint );
}
SplashDialog::~SplashDialog()
{
delete ui;
}
Splash Dialog opens and is being closed 10 seconds later as intended