[SOLVED]QPushButton not properly displayed
Hello. I am having a simple test program, that should run as daemon and at fixed intervals it creates a, let` say a simple dialog winodw. The dialog has 2 buttons, one just destroys the dialog and makes the daemon to loop until the time comes then recreates the dialog again. The other destroys the daemon process. Here is the simple code for the Daemon:
@
#include "Daemon.h"
static FILE* fileToWrite = NULL;
static FILE* fileToRead = NULL;
int readConfigFileAndReturnParameter(const char *filename) {
FILE *fp = fopen(filename, "r");
}
int init_main(int argc, char *argv[]) {
if ( !(fileToWrite = fopen("Log.txt", "w")) ) return 1;
pid_t process_id = 0;
pid_t sid;
process_id = fork();
if ( process_id < 0 ) {
fprintf(stderr, "Failed to fork process.");
exit(0);
}
umask(0); if ( (sid = setsid()) < 0) exit(EXIT_FAILURE); chdir(argv[1]); close(STDIN_FILENO); close(STDOUT_FILENO); close(STDERR_FILENO); int i=0; SimpleGUI *spl = NULL; while ( 1 ) { sleep(2); if ( spl != NULL) continue; fprintf(fileToWrite, "SimpleGUI is destroyed: %p\n", spl); initQtApp(spl, argc, argv); } fclose(fileToWrite); return (0);
}
int initQtApp(SimpleGUI* spl, int argc, char* argv[]) {
QApplication app(argc, argv);
spl = new SimpleGUI(&app);
return app.exec();
}
//for daemon
int main(int argc, char* argv[]) {
init_main(argc, argv);
}
@
And here is the code for the dialog I am using:
@
SimpleGUI::~SimpleGUI() {
fprintf(stdout, "App terminated");
QObject::connect(this->window, SIGNAL(destroyed()),
this->qapp, SLOT(deleteLater()));
delete this->yes; delete this->no; delete this->buttonsLay;
delete this->window;
self = NULL;
}
SimpleGUI::SimpleGUI() { }
SimpleGUI::SimpleGUI(QApplication* app) : qapp(app) {
this->window = new QWidget;
this->window->setMinimumHeight(300);
this->window->setMaximumHeight(300);
this->window->setMinimumWidth(300);
this->window->setMaximumWidth(300);
this->yes = new QPushButton("STOP DAEMON"); this->no = new QPushButton("CONTINUE"); this->yes->setMinimumWidth(100); this->yes->setMaximumWidth(100); this->no->setMinimumWidth(100); this->no->setMaximumWidth(100); this->buttonsLay = new QHBoxLayout(); this->buttonsLay->addWidget(yes); this->buttonsLay->addWidget(no); this->window->setLayout(this->buttonsLay); QObject::connect(no, SIGNAL(clicked()), this->qapp, SLOT(quit()) ); QObject::connect(this->yes, SIGNAL(clicked()), this, SLOT(doSomething()) ); window->show();
}
void SimpleGUI::doSomething() {
delete this;
}
@
The problem is that when first created and let the daemon go, by just click "Continue" the second time the dialog is displayed, the buttons are not properly displayed, I`ll attach a screenshot to see it.
Here is the link:
- dheerendra
I saw the image. I did not understand what is not displayed properly. I saw two buttons. Also I felt you are creating objects again and again. Which is not required. You can keep the window creation only once and display whenever required.
I mean that they are not like buttons, when I first start they are round, and when first closed, the second time the app starts they are like this - a plain text on the window. You can
t tell if they are buttons or not. Your suggestion? Just to show()/hide() the main window by adding some SINGAL/SLOT logic? But the idea is that I dont need the program to occupy the memory and to stay opened. The example was with 2 seconds, but the idea is to be recreated on every day or even more, this is not the original interface, just a test to see if it was working. Am I not freeing the objects properly? In my source I have the logic, that clickning continue it frees the object completely. The daemon`s if knows that the pointer to the GUI is NULL, then it just recreates it. But if I am missing something in object creation/destruction, please point it to me. Regards.
[quote author="Dheerendra" date="1410024356"]I saw the image. I did not understand what is not displayed properly. I saw two buttons. Also I felt you are creating objects again and again. Which is not required. You can keep the window creation only once and display whenever required.[/quote]
OK, I partly fixed the problem. The problem was with the connection of SIGNAL/SLOTS. Here is the code I`ve edited:
@
SimpleGUI::~SimpleGUI() {
fprintf(stdout, "App terminated");
delete yes;
delete no;
delete window;
delete qapp;
}
void SimpleGUI::doSomething() {
delete this;
QObject::connect(qapp, SIGNAL(aboutToQuit()),
window, SLOT(deleteLater()) );
QObject::connect(window, SIGNAL(destroyed()), qapp, SLOT(quit()) );
}
@
And here is how it looks like each time I`ve click CONTINUE:
And the log file report is:
@
[MSG]App created in 0x8324af0
[MSG]Simple gui in 0x831bc78
[MSG]App created in 0x977f808
[MSG]Simple gui in 0x9883558
[MSG]App created in 0x988d4d8
[MSG]Simple gui in 0x98d0be8
[MSG]App created in 0x97f98c0
[MSG]Simple gui in 0x987af30
@
which assumes that each time a new memory is used, so I hope that Qt frees properly everything with that logic.