[Solved] Problem with QComboBox and Focus
-
Hey,
I'm trying to make a simple text editor. I'm using a QGraphicsView, QGraphicsScene and QGraphicsTextItem for this. Changing the font properties, like bold, italic, and so on is done via QToolButtons. If the user clicked on one of the buttons the new font is handed over to the text item and the focus is going back to it, which is working great.
In order to change the font size, I created a QComboBox with some point sizes. Everything is working except the refocus to the text item, if no text is selected.So in my opinion the combo box somehow manages to regain the focus from the text item.
Any ideas how I'm able to circumvent the problem?
Thanks in advance!
Best regards,
matthias
Edit: Here is a minimal example, I'm using Qt 5.1.1
main.cpp
@#include "mainwindow.hpp"
#include <QApplication>int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();return a.exec();
}@
mainwindow.hpp
@#ifndef MAINWINDOW_HPP
#define MAINWINDOW_HPP#include <QMainWindow>
class QGraphicsView;
class QGraphicsTextItem;class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
MainWindow(QWidget *parent = 0);
~MainWindow();public slots:
void changeFont(const QString &);private:
QGraphicsView *view;
QGraphicsTextItem *text;
};#endif // MAINWINDOW_HPP
@mainwindow.cpp
@#include "mainwindow.hpp"#include <QGraphicsView>
#include <QGraphicsPixmapItem>
#include <QGraphicsScene>
#include <QGraphicsTextItem>
#include <QComboBox>
#include <QVBoxLayout>
#include <QDialog>
#include <QTextDocument>#include <QTextCursor>
#include <QTextCharFormat>
#include <QTextFrame>
#include <QPixmap>
#include <QPainter>
#include <QTextFrameFormat>
#include <QFont>
#include <QBrush>MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent),
view(new QGraphicsView(this))
{
setCentralWidget(new QDialog);
QGraphicsScene *scene;
view->setScene(scene = new QGraphicsScene(this));
view->setBackgroundBrush(QBrush(Qt::gray, Qt::SolidPattern));
scene->setSceneRect(0, 0, 1290, 1804);
QPixmap map(1240, 1754);
QPainter painter;
painter.begin(&map);
painter.fillRect(0, 0, 1240, 1754, QBrush(Qt::white));
QGraphicsPixmapItem *pix = new QGraphicsPixmapItem(map);
painter.end();
pix->setPos(25, 25);
scene->addItem(pix);
text = new QGraphicsTextItem(pix);
text->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard | Qt::TextEditable);
text->setFlags(QGraphicsItem::ItemIsFocusable | QGraphicsItem::ItemIsSelectable);
text->setPos(100, 100);
text->setPlainText("test");QFont defaultFont("Arial"); defaultFont.setPointSizeF(12*1.5625); //scale to 150 dpi text->document()->setUseDesignMetrics(true); text->document()->setDefaultFont(defaultFont); text->document()->setTextWidth(1040); QComboBox *box = new QComboBox(this); box->addItems(QStringList() << "" << "8" << "9" << "10" << "11" << "12" << "14" << "16" << "18" << "20" << "22" << "24" << "26" << "28" << "36" << "48" << "72"); box->setCurrentText("12"); QVBoxLayout *layout = new QVBoxLayout; layout->addWidget(box); layout->addWidget(view); centralWidget()->setLayout(layout); connect(box, SIGNAL(currentIndexChanged(QString)), this, SLOT(changeFont(QString)));
}
MainWindow::~MainWindow()
{}
void MainWindow::changeFont(const QString &size)
{
QTextCursor cursor = text->textCursor();
QFont font = cursor.charFormat().font();
font.setPointSizeF(size.toFloat()*1.5625);if (cursor.hasSelection()){ int start = cursor.selectionStart(); int end = cursor.selectionEnd(); QTextCharFormat format; format.setFont(font); cursor.mergeCharFormat(format); cursor.setPosition(start); cursor.setPosition(end, QTextCursor::KeepAnchor); } else { int pos = cursor.position(); QTextCharFormat charFormat = cursor.charFormat(); charFormat.setFont(font); cursor.mergeCharFormat(charFormat); cursor.setPosition(pos); } text->setTextCursor(cursor); text->setFocus(Qt::MouseFocusReason);
}
@project file
@#-------------------------------------------------Project created by QtCreator 2013-10-26T10:52:37
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = TextEditor
TEMPLATE = appSOURCES += main.cpp
mainwindow.cppHEADERS += mainwindow.hpp
@ -
Solved it, by setting the focus policy of the combo box to Qt::NoFocus.
-
Hi and welcome to devnet,
Glad you solved and thanks for sharing !
Please update the thread title prepending [solved] so other forum users may know a solution has been found :)