制作一个简单的文本编辑器来学习 Qt 并加深我的 C++ 技能。 我正在使用 QComboBox 以编程方式创建字体大小下拉菜单。
QComboBox *sizeComboBox = new QComboBox(this);
attributeToolBar->addWidget(sizeComboBox);
for (int i = 8; i <= 48; i += 2) sizeComboBox->addItem(QString::number(i));
connect(sizeComboBox, &QComboBox::currentTextChanged, this, &MainWindow::setFontSize)
所有选项均已填充,但只有第一个选项可见 (8)。
我搜索了互联网,我询问了ChatGPT,并浏览了SO...我似乎找不到问题或解决方案。
如果更多代码有用,下面是 .cpp 文件。
//TextEditor - just a simple text editor with various features
// by Hans Yunge 11/11/2024
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include <QFileDialog>
#include <QToolBar>
#include <QFont>
#include <QFontComboBox>
//Main
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
textEditorToolBar();
MainWindow::initializeDocument();
}
MainWindow::~MainWindow()
{
delete ui;
}
//Actions
void MainWindow::on_actionNew_triggered()
{
}
void MainWindow::on_actionSave_triggered()
{
QString fileName;
if (currentFileName.isEmpty() || currentFileName.isNull()){
fileName = QFileDialog::getSaveFileName(this, "Open File", "C:\\Users\\hyung\\OneDrive\\Documents", "All Files (*)");
} else {
fileName = QFileDialog::getSaveFileName(this, "Open File", currentFileName);
}
QFile file(fileName);
if (file.open(QIODevice::WriteOnly | QIODevice::Text)){
QTextStream out(&file);
out << ui->documentEditor->toHtml();
file.close();
}
}
void MainWindow::on_actionOpen_triggered()
{
QString fileName = QFileDialog::getOpenFileName(this, "Open File", "C:\\Users\\hyung\\OneDrive\\Documents", "All Files (*)");
QFile file(fileName);
currentFileName = fileName;
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream in(&file);
QString tempString = in.readAll();
ui->documentEditor->setHtml(tempString);
file.close();
}
}
void MainWindow::initializeDocument(){
ui->documentEditor->setAcceptRichText(true);
ui->documentEditor->setHtml("<p>Beginning typing at will</p>");
}
void MainWindow::textEditorToolBar()
{
QToolBar *attributeToolBar = addToolBar("Document Attributes");
// Actions
QAction *boldAction = attributeToolBar->addAction("Bold");
QAction *italicAction = attributeToolBar->addAction("Italic");
QAction *underlineAction = attributeToolBar->addAction("Underline");
// Connecting to slots
connect(boldAction, &QAction::triggered, this, &MainWindow::setBold);
connect(italicAction, &QAction::triggered, this, &MainWindow::setItalic);
connect(underlineAction, &QAction::triggered, this, &MainWindow::setUnderline);
// Font Drop Down
QFontComboBox *fontComboBox = new QFontComboBox(this);
attributeToolBar->addWidget(fontComboBox);
connect(fontComboBox, &QFontComboBox::currentFontChanged, this, &MainWindow::setFont);
// Font Size Drop Down
QComboBox *sizeComboBox = new QComboBox(this);
attributeToolBar->addWidget(sizeComboBox);
for (int i = 8; i <= 48; i += 2) sizeComboBox->addItem(QString::number(i));
connect(sizeComboBox, &QComboBox::currentTextChanged, this, &MainWindow::setFontSize);
}
void MainWindow::setItalic(){
QTextCursor cursor = ui->documentEditor->textCursor();
QTextCharFormat format;
format.setFontItalic(!cursor.charFormat().fontItalic());
cursor.mergeCharFormat(format);
}
void MainWindow::setUnderline(){
QTextCursor cursor = ui->documentEditor->textCursor();
QTextCharFormat format;
format.setFontUnderline(!cursor.charFormat().fontUnderline());
cursor.mergeCharFormat(format);
}
void MainWindow::setBold(){
QTextCursor cursor = ui->documentEditor->textCursor();
QTextCharFormat format;
format.setFontWeight(cursor.charFormat().fontWeight() == QFont::Bold ? QFont::Normal : QFont::Bold);
cursor.mergeCharFormat(format);
}
void MainWindow::setFontSize(const QString &size){
bool ok;
int fontSize = size.toInt(&ok);
if (ok && fontSize > 0) {
QTextCursor cursor = ui->documentEditor->textCursor();
QTextCharFormat format;
format.setFontPointSize(fontSize);
cursor.mergeCharFormat(format);
}
}
void MainWindow::setFont(const QFont &font){
QTextCursor cursor = ui->documentEditor->textCursor();
QTextCharFormat format;
format.setFont(font); // Use setFont() to apply the entire QFont, including family
cursor.mergeCharFormat(format);
}
感谢@chehrlic 提供了有效的解决方案。
在Windows上使用,添加对Windows的预处理器操作系统检查,并将样式更改为“windowsvista”(如果为true),解决了眼前的问题。
main.c
#include <QStyleFactory>
// if windows, set this style
#ifdef Q_OS_WIN
if (QStyleFactory::keys().contains("windowsvista")) {
a.setStyle(QStyleFactory::create("windowsvista"));
}
#endif