我的画图项目需要读写自定义虚线样式
{
...
comboBox_penStyle = new QComboBox;
QStringList SL_penStyle;
SL_penStyle << "______" << "----------" << ".........." << "-.-.-.-.-." << "-..-..-..-.." << settings.value("DashPattern", "5,5").toString();
comboBox_penStyle->addItems(SL_penStyle);
ui->mainToolBar->addWidget(comboBox_penStyle);
connect(comboBox_penStyle, SIGNAL(currentIndexChanged(int)), this, SLOT(comboBox_penStyle_changed(int)));
...
}
void MainWindow::comboBox_penStyle_changed(int index)
{
Qt::PenStyle ps = static_cast<Qt::PenStyle>(index + 1);
imageWidget->pen.setStyle(ps);
if (ps == Qt::CustomDashLine) {
QStringList SL = comboBox_penStyle->itemText(index).split(",");
QVector<qreal> pattern;
for (QString s: SL)
pattern << s.toInt();
imageWidget->pen.setDashPattern(pattern);
}
}
void MainWindow::on_action_dashPattern_triggered()
{
QDialog *dialog = new QDialog(this);
dialog->setWindowTitle("虚线样式");
dialog->setFixedSize(200, 100);
QVBoxLayout *vbox = new QVBoxLayout;
QString s = settings.value("DashPattern", "5,5").toString();
QLineEdit *lineEdit = new QLineEdit;
lineEdit->setText(s);
vbox->addWidget(lineEdit);
QPushButton *pushButton_confirm = new QPushButton("确定");
QPushButton *pushButton_cancel = new QPushButton("取消");
QHBoxLayout *hbox = new QHBoxLayout;
hbox->addStretch();
hbox->addWidget(pushButton_confirm);
hbox->addWidget(pushButton_cancel);
hbox->addStretch();
vbox->addLayout(hbox);
dialog->setLayout(vbox);
connect(pushButton_confirm, SIGNAL(clicked()), dialog, SLOT(accept()));
connect(pushButton_cancel, SIGNAL(clicked()), dialog, SLOT(reject()));
if (dialog->exec() == QDialog::Accepted) {
settings.setValue("DashPattern", lineEdit->text());
comboBox_penStyle->setItemText(5, lineEdit->text());
QStringList SL = lineEdit->text().split(",");
QVector<qreal> pattern;
for (QString s: SL)
pattern << s.toInt();
imageWidget->pen.setDashPattern(pattern);
}
}
GitLink | 确实开源