Important: Please read the Qt Code of Conduct -

[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.");

    if ( (sid = setsid()) < 0) exit(EXIT_FAILURE);
    int i=0;
    SimpleGUI *spl = NULL;
    while ( 1 ) {
        if ( spl != NULL) continue;
        fprintf(fileToWrite, "SimpleGUI is destroyed: %p\n", spl);
        initQtApp(spl, argc, argv);
    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->yes =  new QPushButton("STOP DAEMON");
    this->no = new QPushButton("CONTINUE");
    this->buttonsLay = new QHBoxLayout();
    QObject::connect(no, SIGNAL(clicked()),
                     this->qapp, SLOT(quit()) );
    QObject::connect(this->yes, SIGNAL(clicked()),
                     this, SLOT(doSomething()) );


    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:

  • Qt Champions 2017

    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 cant 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.

Log in to reply