Black screen when a loop is in a code
-
wrote on 2 Mar 2024, 20:43 last edited by answermeplz 3 Feb 2024, 20:49
I am new to QT.
Everytime I have a loop (for or while) in my code, the GUI will not show up and there is only black screen.
When loop is commented out, the GUI will show up.
Where is problem?EDIT: GUI will show up after the loop has ended, but I need to run it simultaneously.
int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); for (int i = 0; i <10; i++){ printf("hello"); } ////////////OR while (true){ print("hello\n"); } }
-
I am new to QT.
Everytime I have a loop (for or while) in my code, the GUI will not show up and there is only black screen.
When loop is commented out, the GUI will show up.
Where is problem?EDIT: GUI will show up after the loop has ended, but I need to run it simultaneously.
int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); for (int i = 0; i <10; i++){ printf("hello"); } ////////////OR while (true){ print("hello\n"); } }
wrote on 2 Mar 2024, 20:46 last edited by@answermeplz said in Black screen when a loop is in a code:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
*for (int i = 0; i <10; i++){
printf("hello");}*
OR
while (true){
print("hello\n");
}
}int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); reurn a.exec(); }
-
@answermeplz said in Black screen when a loop is in a code:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
*for (int i = 0; i <10; i++){
printf("hello");}*
OR
while (true){
print("hello\n");
}
}int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); reurn a.exec(); }
wrote on 2 Mar 2024, 20:54 last edited by@JoeCFD I dont understand
-
@JoeCFD I dont understand
wrote on 2 Mar 2024, 20:56 last edited by JoeCFD 3 Feb 2024, 20:57@answermeplz
you need to call the following:
a.exec(); -
wrote on 2 Mar 2024, 21:08 last edited by answermeplz 3 Feb 2024, 21:09
@answermeplz Sure, I call a.exec, I just forgot to put it here.
-
I am new to QT.
Everytime I have a loop (for or while) in my code, the GUI will not show up and there is only black screen.
When loop is commented out, the GUI will show up.
Where is problem?EDIT: GUI will show up after the loop has ended, but I need to run it simultaneously.
int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); for (int i = 0; i <10; i++){ printf("hello"); } ////////////OR while (true){ print("hello\n"); } }
@answermeplz said in Black screen when a loop is in a code:
GUI will show up after the loop has ended, but I need to run it simultaneously.
This will and can't work since for the Qt stuff to work you need a running event loop. Put your work into a separate worker thread and communicate to/from them with signals and slots.
-
I am new to QT.
Everytime I have a loop (for or while) in my code, the GUI will not show up and there is only black screen.
When loop is commented out, the GUI will show up.
Where is problem?EDIT: GUI will show up after the loop has ended, but I need to run it simultaneously.
int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); for (int i = 0; i <10; i++){ printf("hello"); } ////////////OR while (true){ print("hello\n"); } }
wrote on 2 Mar 2024, 22:22 last edited by ChrisW67 3 Feb 2024, 22:23@answermeplz said in Black screen when a loop is in a code:
Everytime I have a loop (for or while) in my code, the GUI will not show up and there is only black screen.
When loop is commented out, the GUI will show up.
Where is problem?For any of Qt's events to operate the main (GUI) thread needs to be executing the Qt event loop whenever it is not actively responding to one of those events. In your code (that I have added a call to QAppplication::exec() in):
int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show();
So far, so good. This bit,
for (int i = 0; i <10; i++){ printf("hello"); }
will run until completion in a matter of milliseconds and drop through to:
a.exec(); }
and your UI will display.
However, if you use this code,
while (true){ print("hello\n"); }
this will never terminate, never fall through to the Qt event loop, and thus never finish displaying the Qt GUI or start responding to user inputs.
EDIT: GUI will show up after the loop has ended, but I need to run it simultaneously.
Simultaneously with what?
-
wrote on 3 Mar 2024, 12:16 last edited by answermeplz 3 Mar 2024, 12:18
Okay, I understand, thank you very much!
I work on Raspberry Pi project, and my goal is to have one thread handling GUI and second thread handling measuring and processing data from sensor.
My goal is to run GUI simultaneously with measuring of data, while GUI is not blocked because of infinite loop.
So correct way would be for example like this?:void sensorfunction(){ while(true){ //processing data -> sending them to GUI std::this_thread::sleep_for(std::chrono::seconds(1)); } } int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); std::thread thread1(sensorfunction); }
-
Okay, I understand, thank you very much!
I work on Raspberry Pi project, and my goal is to have one thread handling GUI and second thread handling measuring and processing data from sensor.
My goal is to run GUI simultaneously with measuring of data, while GUI is not blocked because of infinite loop.
So correct way would be for example like this?:void sensorfunction(){ while(true){ //processing data -> sending them to GUI std::this_thread::sleep_for(std::chrono::seconds(1)); } } int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); std::thread thread1(sensorfunction); }
wrote on 3 Mar 2024, 12:23 last edited by JonB 3 Mar 2024, 12:25@answermeplz
No, because you won't even hitstd::thread thread1(sensorfunction);
statement since it's after areturn
statement! (Compiler might have warned you of this "unreachable statement"?)In some shape of form, you have to kick off the thread before entering
a.exec()
or from within it. Use Qt's threading functions. have a read of e.g. https://wiki.qt.io/QThreads_general_usage for "worker class" pattern. If the thread needs to communicate with the main UI (or opposite direction) thread use signals/slots to communicate/pass data, do not allow worker thread to access UI directly. -
Okay, I understand, thank you very much!
I work on Raspberry Pi project, and my goal is to have one thread handling GUI and second thread handling measuring and processing data from sensor.
My goal is to run GUI simultaneously with measuring of data, while GUI is not blocked because of infinite loop.
So correct way would be for example like this?:void sensorfunction(){ while(true){ //processing data -> sending them to GUI std::this_thread::sleep_for(std::chrono::seconds(1)); } } int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); std::thread thread1(sensorfunction); }
wrote on 3 Mar 2024, 12:31 last edited by Ronel_qtmaster 3 Mar 2024, 12:32@answermeplz The same thread for the gui can support sensor reading, however you will not use while loop, but check for readyRead signal of QSerialPort when the port is opened, process the data.
check this https://doc.qt.io/qt-6/qtserialport-terminal-example.html
1/10