Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] Problem with QComboBox and Focus
Forum Updated to NodeBB v4.3 + New Features

[Solved] Problem with QComboBox and Focus

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 5.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Saugglocke
    wrote on last edited by
    #1

    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&#40;&#41;;
    

    }@

    mainwindow.hpp
    @#ifndef MAINWINDOW_HPP
    #define MAINWINDOW_HPP

    #include <QMainWindow>

    class QGraphicsView;
    class QGraphicsTextItem;

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    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 = app

    SOURCES += main.cpp
    mainwindow.cpp

    HEADERS += mainwindow.hpp
    @

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Saugglocke
      wrote on last edited by
      #2

      Solved it, by setting the focus policy of the combo box to Qt::NoFocus.

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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 :)

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved