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 ?
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);
}