参考
定义枚举值:
https://github.com/cutefishos/dock/blob/main/src/docksettings.h#L36
enum Direction {
Left = 0,
Bottom,
Right
};
Q_ENUMS(Direction)
读取设置:
https://github.com/cutefishos/dock/blob/main/src/docksettings.cpp#L64
Direction m_direction = static_cast<Direction>(m_settings->value("Direction").toInt());
保存设置:
https://github.com/cutefishos/dock/blob/main/src/mainwindow.cpp#L132
void MainWindow::setDirection(int direction)
{
DockSettings::self()->setDirection(static_cast<DockSettings::Direction>(direction));
}
https://github.com/cutefishos/dock/blob/main/src/docksettings.cpp#L90
void DockSettings::setDirection(const Direction &direction)
{
if (m_direction != direction) {
m_direction = direction;
m_settings->setValue("Direction", direction);
emit directionChanged();
}
}
应用到我的画图工具enum
ImageWidget.h
enum DrawType {
NONE_DRAW,
POINT_DRAW,
LINE_DRAW,
ARROW_DRAW,
RECT_DRAW,
ELLIPSE_DRAW,
TEXT_DRAW,
FILL_DRAW,
ERASE_DRAW,
MOVE_DRAW,
SELECT_DRAW,
CLIP_DRAW,
IMAGE_DRAW,
DEL_DRAW,
COLORPICKER_DRAW
};
DrawType drawType;
mainwindow.cpp
void MainWindow::closeEvent(QCloseEvent *event)
{
settings.setValue("DrawType", imageWidget->drawType);
}
void MainWindow::readSettings()
{
imageWidget->drawType = static_cast<ImageWidget::DrawType>(settings.value("DrawType").toInt());
switch (imageWidget->drawType) {
case ImageWidget::POINT_DRAW:
ui->actionPencil->trigger();
break;
case ImageWidget::LINE_DRAW:
ui->actionLine->trigger();
break;
case ImageWidget::ARROW_DRAW:
ui->actionArrow->trigger();
case ImageWidget::RECT_DRAW:
ui->actionRect->trigger();
break;
case ImageWidget::ELLIPSE_DRAW:
ui->actionEllipse->trigger();
break;
case ImageWidget::TEXT_DRAW:
ui->actionText->trigger();
break;
case ImageWidget::COLORPICKER_DRAW:
ui->actionColorPicker->trigger();
break;
case ImageWidget::FILL_DRAW:
ui->actionFill->trigger();
break;
case ImageWidget::ERASE_DRAW:
ui->actionErase->trigger();
break;
case ImageWidget::MOVE_DRAW:
ui->actionMove->trigger();
break;
case ImageWidget::SELECT_DRAW:
ui->actionRectSelect->trigger();
break;
}
}