2

I will start by describing my C++ GUI app.

I have a home screen (mainwindow) which has a number of labels(kpi's) that show various data. And there is a settings button in the home screen, pressing which a settings dialog (consettingsdialog) opens which has various parameter settings for the mainwindow labels and the application itself. The settings parameters are saved after pressing the save button in consettingsdialog.

My target is to make certain labels in the mainwindow visible / invisible if corresponding checkboxes are checked or unchecked in the consettingsdialog.

This is the details what I have done so far;

in my consettingsdialog.cpp

ConSettingsDialog::ConSettingsDialog(QWidget *parent)
      : QDialog(parent)
{
    setupUi(this);
    kpi1CheckBox->setChecked(true);
}

void ConSettingsDialog :: onSaveButton()
{
if(kpi1CheckBox->isChecked()==true)
    {
        kpi1CheckBox->setChecked(true);
        emit setHomeScreenKpiVisibility();     
    }
    else
    {
        kpi1CheckBox->setChecked(false);
        emit setHomeScreenKpiInvisibility();     
    }
}

in my mainwindow.cpp

MainWindow::MainWindow(QWidget *parent):
        QMainWindow(parent), ui(new Ui::MainWindow)
{
     ui->setupUi(this);
     m_ConSettingsDialog =new ConSettingsDialog();                  

    connect(m_ConSettingsDialog,SIGNAL(setHomeScreenKpiVisibility()),this,SLOT(setHomeScreenKpiVisibility()));     
    connect(m_ConSettingsDialog,SIGNAL(setHomeScreenKpiInvisibility()),this,SLOT(setHomeScreenKpiInvisibility()));
}

void MainWindow::setHomeScreenKpiVisibility()               
{

    ui->mylabel->setVisible(true);
}
void MainWindow::setHomeScreenKpiInvisibility()               
{

    ui->mylabel->setVisible(false);

}

The code builds up perfectly without any errors, but when I run it no matter how many times I uncheck the checkbox, the label stays visible. And when I open the consettings I see the checkbox is checked.

I tried by changing kpi1CheckBox->setChecked(true); in the consettingsdialog.cpp to kpi1CheckBox->setChecked(false); but then also the label in home screen stays visible (not invisible at all). Although in this case the checkbox in settings dialog is set permanently disabled.

Feeling completely stuck now, what am I doing wrong ?

RicoRicochet
  • 2,249
  • 9
  • 28
  • 53
  • On setHomeScreenKpiVisibility and setHomeScreenKpiInvisibility methods, why do you create a new dialog altogether?. Aren't you supposed to set the label visibility for m_ConSettingsDialog itself? – sajas Jan 29 '15 at 05:27
  • When do you actually call the onSaveButton method in ConSettingsDialog? – sajas Jan 29 '15 at 06:01
  • You could try putting a break point in the onSaveButton method and check if it's getting called ? – sajas Jan 29 '15 at 06:06
  • yup.. it is getting called alright.. cuz the rest of the setting parameters in that page are working after savebutton is clicked... – RicoRicochet Jan 29 '15 at 06:31
  • The code that you have posted is working for me. One mistake that I did while I was trying it out was that, I forgot to declare the setHomeScreenKpiVisibility & setHomeScreenKpiInvisibility as slot. I was facing the similar issue that you faced at that time. But after resolving that the code works fine. One more thing, Is the setChecked call in MainWindow::OnSaveButton required. You have already set it when you clicked on it, right? – sajas Jan 29 '15 at 06:58
  • there is no OnSaveButton to the class MainWindow; it is in ConSettingsDialog. I tried by commenting out the setChecked calls, still nothing happening as desired. – RicoRicochet Jan 29 '15 at 07:04
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/69791/discussion-between-sajas-and-ricoricochet). – sajas Jan 29 '15 at 07:06

1 Answers1

1

When you calling onSaveButton() ?, I try read your code, it not wrong, but when you checked, I thinks you must call onSaveButton() method. You can using :

connect(kpi1CheckBox,SIGNAL(toggled(bool)),this,SLOT(onSaveButton(bool)));

Please make sure you connect for all checkbox.

Add Example: dialog.h

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>

namespace Ui {
    class Dialog;
}

class Dialog : public QDialog
{
    Q_OBJECT

public:
    explicit Dialog(QWidget *parent = 0);
    ~Dialog();

private:
    Ui::Dialog *ui;

private slots:
    void onsave(bool checked);
signals:
    void setHomeScreenKpiVisibility();
    void setHomeScreenKpiInvisibility();
};

#endif // DIALOG_H

Dialog.cpp

#include "dialog.h"
#include "ui_dialog.h"

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
    connect(ui->checkBox,SIGNAL(toggled(bool)),this,SLOT(onsave(bool)));
}

Dialog::~Dialog()
{
    delete ui;
}

void Dialog::onsave(bool checked)
{
    if(ui->checkBox->isChecked()==true)
        {
            ui->checkBox->setChecked(true);
            emit setHomeScreenKpiVisibility();
        }
        else
        {
            ui->checkBox->setChecked(false);
            emit setHomeScreenKpiInvisibility();
        }
}

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
    class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
private slots:
    void show1();
    void enable1();
    void disable1();
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "dialog.h"
Dialog *dialog;
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    dialog = new Dialog(this);
    connect(ui->pushButton,SIGNAL(clicked()),dialog,SLOT(show()));
    connect(dialog,SIGNAL(setHomeScreenKpiVisibility()),this,SLOT(enable1()));
    connect(dialog,SIGNAL(setHomeScreenKpiInvisibility()),this,SLOT(disable1()));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::show1()
{
    dialog->exec ();
}

void MainWindow::enable1()
{
    ui->label->setEnabled (true);
}

void MainWindow::disable1()
{
    ui->label->setEnabled (false);
}
Hien Ngo
  • 253
  • 5
  • 18
  • well, i tried but still nothing happened. its same as earlier. and i have a further doubt, when i am connecting the signals to slot, the **this** belongs to the class of mainwindow right ??? and there are no **SaveButton** in my mainwindow. Correct me if i am wrong in my understanding. – RicoRicochet Jan 29 '15 at 05:51
  • No, "THIS" this is 'ConSettingsDialog', when you check on 'kpi1CheckBox', slot 'onSaveButton' is called, and it will check 'kpi1CheckBox' state, and emit signal to mainwindow. Do you understand ? – Hien Ngo Jan 29 '15 at 06:03
  • hmm.. okay, but it is still not working functionally.. any more insight ?? – RicoRicochet Jan 29 '15 at 06:58
  • See example I Add in my post. – Hien Ngo Jan 29 '15 at 07:30