gpt4 book ai didi

c++ - Qt,在构造函数之外修改小部件属性的问题

转载 作者:行者123 更新时间:2023-11-28 07:46:17 26 4
gpt4 key购买 nike

我想知道是否有人可以告诉我这里到底发生了什么。我已经包含了一个简单配置对话框的头文件和实现文件。问题出在 updateAutoSaveGroupBox 插槽中,我无法访问或更改页面上小部件的任何属性。 IE。如果未选中复选框,我想禁用某些小部件,但是当我尝试设置它们时,我遇到了读取访问冲突。对此事的任何帮助将不胜感激。问题存在于这两行(注释掉所以它现在会运行而不会抛出异常)。

//autoSaveLabel->setDisabled(autoSaveIsEnabled);
//autoSaveSpinBox->setDisabled(getAutoSaveIsEnabled());

配置控件.h

class EnigmaConfigGeneralEnvironmentWidget : public QWidget
{
Q_OBJECT
public:
explicit EnigmaConfigGeneralEnvironmentWidget(QWidget *parent = 0);
~EnigmaConfigGeneralEnvironmentWidget();

signals:
void setAutoSaveIsEnabledSignal(bool autoSaveIsEnabled);

public slots:
void setAutoSaveIsEnabled(bool autoSaveIsEnabled){_AutoSaveIsenabled = autoSaveIsEnabled;}
bool getAutoSaveIsEnabled(){return _AutoSaveIsenabled;}

void updateAutoSaveGroupBox(bool autoSaveIsEnabled);

private:
void makeConnections();
void readSettings();
void writeSettings();
void createMainWidget();

QGroupBox *uiGroupBox;
QStringList localList;
QLabel *localLabel;
QComboBox *localeComboBox;
QHBoxLayout *localSelectionHLayout;
QGroupBox *systemGroupBox;
QCheckBox *autoSaveCheckBox;
QLabel *autoSaveLabel;
QSpinBox *autoSaveSpinBox;
QHBoxLayout *autoSaveHLayout;

bool _AutoSaveIsenabled;
};

ConfigWidget.cpp

#include "enigmaconfiggeneralenvironmentwidget.h"

#include <QtWidgets>

EnigmaConfigGeneralEnvironmentWidget::EnigmaConfigGeneralEnvironmentWidget(QWidget *parent) :
QWidget(parent)
{
makeConnections();
readSettings();

createMainWidget();
}

EnigmaConfigGeneralEnvironmentWidget::~EnigmaConfigGeneralEnvironmentWidget()
{
writeSettings();
}

void EnigmaConfigGeneralEnvironmentWidget::makeConnections()
{connect(this,SIGNAL(setAutoSaveIsEnabledSignal(bool)),this,SLOT(setAutoSaveIsEnabled(bool)) );
connect(this,SIGNAL(setAutoSaveIsEnabledSignal(bool)),this,SLOT(updateAutoSaveGroupBox(bool)) );
}

void EnigmaConfigGeneralEnvironmentWidget::readSettings()
{
QSettings settings;
settings.beginGroup(tr("UI.Config.Environment.General"));
bool autoSaveIsEnabled = settings.value("autoSaveIsEnabled",bool(true)).toBool();
setAutoSaveIsEnabledSignal(autoSaveIsEnabled);
settings.endGroup();
}

void EnigmaConfigGeneralEnvironmentWidget::writeSettings()
{
QSettings settings;
settings.beginGroup(tr("UI.Config.Environment.General"));
settings.setValue("autoSaveIsEnabled",getAutoSaveIsEnabled());
settings.endGroup();
}

void EnigmaConfigGeneralEnvironmentWidget::createMainWidget()
{
localList.append(tr("Danish - ???"));
localList.append(tr("English - Australia"));
localList.append(tr("English - Canada"));
localList.append(tr("English - USA"));
localList.append(tr("English - UK"));
localList.append(tr("Finnish - Finland"));
localList.append(tr("French - Canada"));
localList.append(tr("French - France"));
localList.append(tr("Norwegian - ???"));
localList.append(tr("Swedish - ???"));

uiGroupBox = new QGroupBox();
uiGroupBox->setTitle(tr("UI Settings"));

localLabel= new QLabel();
localLabel->setText(tr("Select a language: "));

localeComboBox = new QComboBox();
localeComboBox->addItems(localList);

localSelectionHLayout = new QHBoxLayout(uiGroupBox);
localSelectionHLayout->addWidget(localLabel);
localSelectionHLayout->addWidget(localeComboBox);

systemGroupBox = new QGroupBox();
systemGroupBox->setTitle(tr("System Settigns"));

autoSaveCheckBox = new QCheckBox();
autoSaveCheckBox->setText(tr("Auto-Save Enabled: "));
autoSaveCheckBox->setChecked(getAutoSaveIsEnabled());
connect(autoSaveCheckBox,SIGNAL(clicked(bool)),this,SIGNAL(setAutoSaveIsEnabledSignal(bool)));
autoSaveLabel = new QLabel(this);
autoSaveLabel->setText(tr("Auto-Save Interval is Every: "));

autoSaveSpinBox = new QSpinBox();
autoSaveSpinBox->setSuffix(tr("Mins."));
autoSaveSpinBox->setAccelerated(true);

autoSaveHLayout = new QHBoxLayout(systemGroupBox);
autoSaveHLayout->addWidget(autoSaveCheckBox);
autoSaveHLayout->addWidget(autoSaveLabel);
autoSaveHLayout->addWidget(autoSaveSpinBox);

QVBoxLayout *vLayout = new QVBoxLayout(this);
vLayout->addWidget(uiGroupBox);
vLayout->addWidget(systemGroupBox);
}

void EnigmaConfigGeneralEnvironmentWidget::updateAutoSaveGroupBox(bool autoSaveIsEnabled)
{
qDebug() << "debug " << autoSaveIsEnabled;
//autoSaveLabel->setDisabled(autoSaveIsEnabled);
//autoSaveSpinBox->setDisabled(getAutoSaveIsEnabled());
}

最佳答案

发现它:

void EnigmaConfigGeneralEnvironmentWidget::readSettings()
{
QSettings settings;
settings.beginGroup(tr("UI.Config.Environment.General"));
bool autoSaveIsEnabled = settings.value("autoSaveIsEnabled",bool(true)).toBool();
setAutoSaveIsEnabledSignal(autoSaveIsEnabled);
settings.endGroup();
}

您在这里调用 void setAutoSaveIsEnabledSignal(bool)(信号),而不是 void setAutoSaveIsEnabled(bool)(实际 setter )。因此成员变量仍未初始化。

提醒自己:不要将信号命名为 setter,使用例如void autoSaveIsEnabledChanged(bool)

关于c++ - Qt,在构造函数之外修改小部件属性的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14823670/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com