Connecting to a same signal in two windows - c++

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.

Related

Child window elements are blocked in Qt 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();

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.

How to Connect Signal from MainWindow to Slot in Dialog

I have signal in my MainWindow to emit a number thats in a line edit. When I click a button to open the dialog, I want that number to be copied to the line edit in the dialog. I can't get it to connect. I can see the signal is emitting with qDebug. Am I connecting it wrong or in the wrong place? I have tried many ways. Here are my code snippets.
Mainwindow
//My MainWindow
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
ui->setupUi(this);
//This is the number I am trying to send to the dialog
ui->checkingAmount->setText(QString::number(1000.00, 'f', 2));
ui->checkingAmount->setReadOnly(true);
}
//Emit the data here
void MainWindow::on_transferButton_clicked() {
transferWindow = new TransferWindow(this);
transferWindow->show();
//trying to emit the data
QString data =ui->checkingAmount->text();
emit shareCheckingData(data);
qDebug()<<"emitting mainwin amount";
}
Dialog
//My Dialog
TransferWindow::TransferWindow(QWidget *parent) : QDialog(parent),ui(new Ui::TransferWindow) {
ui->setupUi(this);
//I have tried several variations of this
//mainWindow = new MainWindow();
connect(mainWindow, SIGNAL(shareCheckingData(QString)),this, SLOT(getAmountFromMainWin(QString)));
}
//Here is the connecting slot to get the data from main window
void TransferWindow::getAmountFromMainWin(QString n) {
float CheckTotal = n.toFloat();
ui->checkingAmount->setReadOnly(true);
ui->checkingAmount->setText(QString::number(CheckTotal));
qDebug()<<"setting amount";
}
How can I get this to connect? I read through many posts and it did not solve the problem. Thanks.
I notice in the comments of your code that you intend to create an instance of MainWindow and try to connect to this instance, that is a new instance different from the previous one so you will not be able to get it.
First we must create the instance and then connect it, that we can do in the constructor.
MainWindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->checkingAmount->setText(QString::number(1000.00, 'f', 2));
ui->checkingAmount->setReadOnly(true);
transferWindow = new TransferWindow(this);
connect(this, &MainWindow::shareCheckingData, transferWindow, &TransferWindow::getAmountFromMainWin);
//old style
//connect(this, SIGNAL(shareCheckingData(QString)), transferWindow, SLOT(getAmountFromMainWin(QString)));
}
void MainWindow::on_transferButton_clicked()
{
//trying to emit the data
QString data =ui->checkingAmount->text();
emit shareCheckingData(data);
qDebug()<<"emitting mainwin amount";
transferWindow->show();
}
TransferWindow.cpp
TransferWindow::TransferWindow(QWidget *parent) :
QDialog(parent),
ui(new Ui::TransferWindow)
{
ui->setupUi(this);
}
void TransferWindow::getAmountFromMainWin(QString n)
{
float CheckTotal = n.toFloat();
ui->checkingAmount->setReadOnly(true);
ui->checkingAmount->setText(QString::number(CheckTotal));
qDebug()<<"setting amount";
}

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

Controlling Multiple UI files in Qt framework

Question asked twice: see Handling multiple ui files in Qt
I am new to Qt framwork , i have been given this simple task:
In the MainWindow , i have a submit button , once its clicked another total different window should appear
i thought of doing this by doing one extra UI file called From.ui file and to switch from MainWindow to Form once submit is clicked , this is my code:
//main.cpp
#include "mainwindow.h"
#include <QtGui/QApplication>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow mainWindow;
mainWindow.setOrientation(MainWindow::ScreenOrientationAuto);
mainWindow.showExpanded();
return app.exec();
}
//MainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "form.h"
#include <QtCore/QCoreApplication>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow:: SubmitClicked()
{
Form* f= new Form(this);
f->show();
f->raise();
f->activateWindow();
}
//Form.cpp
#include "form.h"
#include "ui_form.h"
Form::Form(QWidget *parent) :
QWidget(parent),
ui(new Ui::Form)
{
ui->setupUi(this);
}
Form::~Form()
{
delete ui;
}
this code compiled perfectly , but its not doing as expected , once submit is clicked , nothing is done...
can you please tell me whats wrong ?
It seems that SubmitClicked slot is not connected to clicked event of your button
Put a cout/printf at the top of your SubmitClicked method to make sure that it is called.