Why MainWindow in Qt is closing after start the program? - linux

I was working on Windows with this program and was ok..
When im running it on linux (using QT creator, same on windows) window show and hide immediately.
The program is to big to paste it all, this is my main.cpp code
int main(int argc, char *argv[]){
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
What should it be?

If there is no fatal exception that exits the app then the event loop (a.exec()) is quitting due to some event served. So, the visibility is not enough to conclude why your app is quitting on Linux.

Related

How to rotate a Qt5 application using the linux framebuffer?

I have an embedded linux application running directly on the linux framebuffer (no x-Windows). We now have to physically rotate the display 180 degrees. How do I get my Qt application to rotate so it doesn't appear upside down? I saw reference to using the following option:
-platform linuxfb:fb=/dev/fb0:rotation:180
However, the rotation option seems to be ignored.
Using Qt 5.9.2 on Ubuntu server 16.04.6
You could handle it on application level. With QML thats easy, but with QWidgets the best I could come up with is to render the Widget on a QGraphicsScene and rotate it like this:
#include "mainwindow.h"
#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
QGraphicsScene *scene = new QGraphicsScene();
QGraphicsView *view = new QGraphicsView();
view->setGeometry(w.geometry());
view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
scene->addWidget(&w);
view->setScene(scene);
view->show();
view->rotate(180);
//w.show();
return a.exec();
}
It seems a bit glitchy, but you could give it a try.
Also I think the correct syntax is -platform linuxfb:fb=/dev/fb0:rotation=180 note the = instead of :
Edit: but that did not make a difference for me either.

Why does gtkmm automatically create a second thread sometimes?

If I compile and run the code as-is, the process will run with 1 thread. If I uncomment the commented out section and then compile and run it, it runs with 2 threads.
I am compiling the code with this command:
g++ pkg-config gtkmm-2.4 --cflags --libs test.cpp
When the program is running I can check how many threads are created with:
ps -mC a.out
If I look at the second thread in ddd, I can see that it is running g_main_loop_run. This confuses me:
What is the purpose of this thread?
Why does adding a toolbar button create a new thread?
I thought g_main_loop_run() should only ever run in one thread (unless you use the GDK_THREADS_ENTER/GDK_THREADS_LEAVE macros). Since I am running Gtk::Main::Run() in my main thread am breaking the rules?
Thanks in advance for any help. It's driving me crazy.
#include <gtkmm.h>
bool OnDeleteEvent(GdkEventAny* PtrGdkEventAny)
{
Gtk::Main::quit();
return(true);
}
void OnExecuteButtonClicked()
{
}
int main(int argc, char *argv[])
{
new Gtk::Main(0, NULL);
Gtk::Window *ptrWindow = new Gtk::Window;
ptrWindow->signal_delete_event().connect(sigc::ptr_fun(&OnDeleteEvent));
/*
Gtk::Toolbar *ptrToolBar = manage(new Gtk::Toolbar);
Gtk::ToolButton *ptrToolButton;
ptrToolButton = manage( new Gtk::ToolButton(Gtk::Stock::EXECUTE));
ptrToolBar->append(*ptrToolButton, sigc::ptr_fun(&OnExecuteButtonClicked));
ptrWindow->add(*ptrToolBar);
*/
ptrWindow->show_all();
Gtk::Main::run();
return (0);
}
Sometimes GThreads are created when you use functions that rely on async behaviour. These usually create a GTask internally (with g_task_run_in_thread and friends) and run the synchronous version in a seperate thread (except for those being nativly async or async-able, those usually won't spawn another thread). Usually this is IO (i.e. GtkBuilder), Socket and IPC (dbus) related - so mostly glib stuff.
There might also be occasions which I am not aware of, that will spawn additional threads, the mainloop itself is strictly single threaded.
So in your case I can only think of two thing that could trigger this: The Stock image that is loaded from your local disk or the styling information of your theme.

Error Displaying Windows using Boost Thread & OpenCV OSX

So I am aware of the following link: Problem accessing camera when using Boost thread on OSX
But my issue is that I cannot display windows when using openCV from within a boost thread. I.e:
int main(int argc, char* argv[]) {
CvCapture* cvInputObj = cvCaptureFromCAM((CV_CAP_ANY)); //OSX
assert( cvInputObj != NULL ); //term on fail here
cVision vision(cvInputObj); //Define cVision thread obj
boost::thread cVision_thd(boost::bind(&cVision::Run, &vision));
cVision_thd.join();
std::cout<<"System Going Down..."<<std::endl;
}
In cVision I do a bunch of openCv calls such as for ex:
cvNamedWindow("MONITOR", CV_WINDOW_AUTOSIZE);
cvShowImage("MONITOR", imCur);
etcetc, where imCur is extracted out as such:
imCur = cvQueryFrame(input);
This exact code works perfectly in Linux. I have originally tried this with a custom makefile. That did not work. After this I tried with CMAKE. Still the same result. And still works fine in linux. I installed openCV using homebrew.
Create the window in the main thread, and then pass the name of the window as a parameter to cVision constructor.
Remember, the window is created with: cvNamedWindow("MONITOR", CV_WINDOW_AUTOSIZE);

How come console app doesn't end?

My first time working with Visual C++ (new to the language, too) - experienced C# ...so I have my first console app that I started in Visual Studio.
#include "stdafx.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int i;
cin >> i;
return 0;
}
How come the console window - thus the application - doesn't close when I press enter? No other input - just enter ...
More importantly, how can I make the app exit (don't want to use exit()) properly if I just hit enter?
std::cin awaits one non-empty string from you and then tries to convert this string to integer.
When you press Enter std::cin only gets an empty string and continues to wait for some valid input. This is by design. std::cin is not meant for emulating other interaction.
To terminate the app on keypress, you have to use OS-specific facilities to read keyboard presses.
This is the kbhit() function from "conio.h" on DOS/console Windows and the termio functions on POSIX systems.
From your source I can conclude that you use the MSVC++ compiler, so try replacing
std::cin >> i
by
while(!kbhit()) {}
Do not forget to add the
#include <conio.h>
and remember this is a Windows-specific solution.

Is it possible to run the main QT program on a thread?

I have a simple QT object. When I execute the code below the control is transfer to the QT object, but I would like to make the QT part work like a thread.
int main(int argc, char *args[])
{
gui *GUI;
//// before call
QApplication app(argc,args);
GUI = new gui();
GUI->show();
////i want to be able to do stuff here in parallel with the QT code.
// If I spawn a thead here or give a simple printf statement here
// or before call it executes only after GUI exits
return app.exec();
}
Make sure you not only create the thread but actually start it. Also, a printf() statement will execute before the GUI shows unless you forgot to terminate the string with a newline (\n).

Resources