Элементы QCombobox невидимы - виден только первый элементC++

Программы на C++. Форум разработчиков
Ответить Пред. темаСлед. тема
Anonymous
 Элементы QCombobox невидимы - виден только первый элемент

Сообщение Anonymous »

Создаю простой текстовый редактор для изучения Qt и углубления своих навыков C++. Я создаю раскрывающийся список размеров шрифта программно с помощью QComboBox.
QComboBox *sizeComboBox = new QComboBox(this);
attributeToolBar->addWidget(sizeComboBox);
for (int i = 8; i addItem(QString::number(i));
connect(sizeComboBox, &QComboBox::currentTextChanged, this, &MainWindow::setFontSize)

Все параметры заполнены, но виден только первый (8).
Изображение

Я порылся в Интернете, спросил ChatGPT и просмотрел ТАК... Я не могу похоже, не нашел ни проблемы, ни решения.
Если вам полезен дополнительный код, ниже приведен файл .cpp.
//TextEditor - just a simple text editor with various features
// by Hans Yunge 11/11/2024

#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include
#include
#include
#include

//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 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("
Beginning typing at will
");
}

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


Подробнее здесь: https://stackoverflow.com/questions/791 ... em-visible
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «C++»