Decide which Window open in QT on start - c++

I need help. I'm trying to open 1 of 2 possible windows on start. Program decide which window will open on screen dimensions.
#include <QApplication>
#include <QDesktopWidget>
#include "mainwindow.h"
#include "vincellform.h"
#include <QDebug>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QDesktopWidget mydesk;
if (mydesk.screenGeometry().width() == 800 && mydesk.screenGeometry().height() == 480)
{
VincellForm vf;
vf.show();
}
else
{
MainWindow w;
w.show();
}
return a.exec();
}
I think that this code is correct, but it isn't. If I'm on different screen (1280*1024 I think) program goes to else part (MainWindow w; w.show();) and then goes to return, but no window is opened. But if I changed a code to:
#include <QApplication>
#include <QDesktopWidget>
#include "mainwindow.h"
#include "vincellform.h"
#include <QDebug>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QDesktopWidget mydesk;
if (mydesk.screenGeometry().width() == 800 && mydesk.screenGeometry().height() == 480)
{
VincellForm vf;
vf.show();
}
MainWindow w;
w.show();
return a.exec();
}
it runs perfectly (MainWindow will open after return). I can't even imagine where the problem can be... Thank you very much

You're defining the window variables locally in the if and else blocks. This means the windows are destroyed immediately after they're shown.
You have two solutions. If you don't mind creating both windows, but only showing one, do this:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QDesktopWidget mydesk;
VincellForm vf;
MainWindow w;
if (mydesk.screenGeometry().width() == 800 && mydesk.screenGeometry().height() == 480)
{
vf.show();
}
else
{
w.show();
}
return a.exec();
}
If you only want one of them created, you'll have to resort to dynamic allocation:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QDesktopWidget mydesk;
std::unique_ptr<VincellForm> vf;
std::unique_ptr<MainWindow> w;
if (mydesk.screenGeometry().width() == 800 && mydesk.screenGeometry().height() == 480)
{
vf.reset(new VincellForm);
vf->show();
}
else
{
w.reset(new MainWindow);
w->show();
}
return a.exec();
}
Note: std::unique_ptr comes from C++11. If you don't have this yet, use raw pointers instead a delete manually at program end.

Related

I don‘t have member function installNativeEventFilter

That's strange, w have the member function installEventFilter()
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char* argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.SerialPortInit();
//w.installNativeEventFilter(&w);
w.installNativeEventFilter(&w);
w.show();
qDebug() << "hello Wolrd";
return a.exec();
}
enter image description here

How to end graphic GUI and do console function(QT)?

I am completely new to QT and I want to prepare one window and take some input from the user then with this input run one console and show output in the console. I have tried to write code after exec but it seems it is not possible:
int main(int argc, char *argv[])
{
int retmain = 0;
QApplication a(argc, argv);
MainWindow w;
w.show();
cout<<"pos500"<<endl;
retmain = a.exec();
cout<<"pos50"<<endl;
//doing something
return retmain;
}
I don't know why but after a.exec(); nothing happens.
So I searched on the internet and found below topic in stackoverflow:
How to call function after window is shown?
But I want to end the graphic window and then do my process.
You need to call QCoreApplication::exit() to make exec return control to you.
After this function has been called, the application leaves the main event loop and returns from the call to exec(). The exec() function returns returnCode. If the event loop is not running, this function does nothing.
A simple example would be:
//mainwindow.h
//////////////////////////////////////////////////
#pragma once
#include <QtWidgets/QMainWindow>
#include <QtCore/QCoreApplication>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
void closeEvent(QCloseEvent *event);
~MainWindow();
};
//mainwindow.cpp
//////////////////////////////////////////////////
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
}
void MainWindow::closeEvent(QCloseEvent *event)
{
QCoreApplication::exit(0);
QMainWindow::closeEvent(event);
}
MainWindow::~MainWindow(){}
//main.cpp
//////////////////////////////////////////////////
#include "mainwindow.h"
#include <QApplication>
#include <iostream>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
a.exec();
std::cout << "test" << std::endl;
return 0;
}

Preserve QbyteArray to Qstring

I want to display the value of QbyteArray like how qDebug() displays it.
qDebug()<<byteArray === Displays -> "\x8E\xA9\xF3\xA5"
how do you grab this QbyteArray into a QString, when i do the convertion found online it gives me "????" as an output .
I would like the content of the QString is the same as the output of the QDebug();
"\x8E\xA9\xF3\xA5"
so that
QString string would contain "\x8E\xA9\xF3\xA5"
Build a QDebug object using the constructor:
QDebug::QDebug(QString *string)
Constructs a debug stream that writes to the given string.
Example:
#include <QApplication>
#include <QDebug>
#include <QLabel>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QLabel label;
QByteArray ba("\x8E\xA9\xF3\xA5");
QString res;
QDebug db(&res);
db << ba;
label.setText(res);
label.show();
return a.exec();
}
Update:
without "\x", use toHex():
#include <QApplication>
#include <QLabel>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QLabel label;
QByteArray ba("\x8E\xA9\xF3\xA5");
label.setText(ba.toHex());
label.show();
return a.exec();
}

QWebEnginePage does not know its contents size

I have a very simple application using WebEngineView and I just wanted to resize the widget displaying to the contents of the html file. I'm expecting it to be 30 pixels wide. Instead my program prints QSize(0,0) and even worser the widget is not displayed at all.
What I'm doing wrong here?
#include <QWebEngineView>
#include <QApplication>
#include <QDebug>
#include <QWebEnginePage>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
auto view = new QWebEngineView;
QString html = "<html><body><div width=30px>Text</div></body></html>";
view->setHtml(html);
auto contentsSize=view->page()->contentsSize().toSize();
qDebug() << contentsSize;
view->setFixedSize(contentsSize);
view->show();
return app.exec();
}
Putting my QWebEngineView into a dialog still doesn't work:
#include <QWebEngineView>
#include <QApplication>
#include <QDebug>
#include <QDialog>
#include <QHBoxLayout>
#include <QWebEnginePage>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
auto dialog = new QDialog;
dialog->setLayout(new QHBoxLayout);
auto view = new QWebEngineView;
dialog->layout()->addWidget(view);
QString html = "<html><body><div width=30px>Text</div></body></html>";
view->setHtml(html);
auto contentsSize=view->page()->contentsSize().toSize();
qDebug() << contentsSize;
view->setFixedSize(contentsSize);
dialog->show();
return app.exec();
}
I also tried to connect to the signal loadFinished, but there is no effect.
#include <QWebEngineView>
#include <QApplication>
#include <QDebug>
#include <QDialog>
#include <QHBoxLayout>
#include <QWebEnginePage>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
auto dialog = new QDialog;
dialog->setLayout(new QHBoxLayout);
auto view = new QWebEngineView;
dialog->layout()->addWidget(view);
QString html = "<html><body><div width=30px>Text</div></body></html>";
view->setHtml(html);
QObject::connect(view->page(), &QWebEnginePage::loadFinished, [&view](bool b) {
qDebug() << b;
auto contentsSize = view->page()->contentsSize().toSize();
qDebug() << contentsSize;
view->setFixedSize(contentsSize);
});
dialog->show();
return app.exec();
}

creating QApplication in a different thread

I'm trying to create QApplication in a different thread, but found 2 main problems:
1- I can't interact with GUI
2- some warnings:
WARNING: QApplication was not created in the main() thread.
QObject::startTimer: timers cannot be started from another thread //happens when resizing widget
QObject::killTimer: timers cannot be stopped from another thread
here is the full code: (it may has some memory leaks but for testing purposes it fails)
//main.cpp
#include <QCoreApplication>
#include "cthread.h"
int main(int argc, char *argv[])
{
CThread *MyThread = new CThread;
MyThread->start();
QCoreApplication a(argc, argv);
return a.exec();
}
//CThread.h
#ifndef CTHREAD_H
#define CTHREAD_H
#include <QThread>
#include "theqtworld.h"
class CThread : public QThread
{
Q_OBJECT
public:
CThread();
void run( void );
private:
TheQtWorld *mWorld;
};
#endif // CTHREAD_H
//CThread.cpp
#include "cthread.h"
#include <iostream>
CThread::CThread():mWorld(NULL)
{
}
void CThread::run()
{
std::cout << "thread started" << std::endl;
if(!mWorld)
mWorld = new TheQtWorld();
mWorld->OpenWorld();//now it will init all Qt Stuff inside
// if(mWorld) delete mWorld;
// emit this->exit();
}
//theqtworld.h
#ifndef THEQTWORLD_H
#define THEQTWORLD_H
#include <QObject>
#include "mainwindow.h"
#include <QApplication>
class TheQtWorld : public QObject
{
Q_OBJECT
public:
explicit TheQtWorld(QObject *parent = 0);
int OpenWorld(void);
signals:
public slots:
};
#endif // THEQTWORLD_H
//theqtworld.cpp
#include "theqtworld.h"
TheQtWorld::TheQtWorld(QObject *parent) :
QObject(parent)
{
}
int TheQtWorld::OpenWorld()
{
static int arg = 0;
static char *b[2];
b[0] = "a";
QApplication *a = new QApplication(arg, b);
a->setParent(this);
MainWindow w;
w.show();
return a->exec();
}
I would answer my own question after understanding how to overcome this problem
first the problem was to integrate Qt GUI as a plugin into another Application, so the main issue was the Event loop collision between Qt Events and any other Application Events
my first thoughts was to separate both, so QApplication will stay at a different thread, but this was a totally wrong approach and here is what I have noticed:
1- Qt GUI Must stay in the main() thread so there is no other place for QApplication
2- to avoid the blocking QApplication::exec() , embed QApplication::processEvents() into the other Application Event loop
here is a working code:
//main.cpp
#include <QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication mApp(argc, argv);
MainWindow w;
w.show();
//just for testing and holding the program so it doesn't end
for(int i = 0; i < 100000000; ++i)
{
mApp.processEvents();
}
return 0;
}
edit:thanks to pavel-strakhov for his great suggestion.