Wiget is not updated till I minimize/maximize window



  • In the following code:

    @#ifndef STEXTBROWSER_H
    #define STEXTBROWSER_H

    #include <QPoint>
    #include <QTextBrowser>

    class PaintedTextBrowser : public QTextBrowser
    {

    private:
    const int LINE_GRAB = 10;
    int separatorCharNum = 10;
    int lineXPos;
    QPoint dragPos;
    QPixmap linePix;

    public:
    PaintedTextBrowser(QWidget *parent = 0);

    void paintEvent(QPaintEvent *ev);
    void mousePressEvent(QMouseEvent *ev);
    void mouseMoveEvent(QMouseEvent *ev);
    void dragEnterEvent(QDragEnterEvent *ev);
    void dragMoveEvent(QDragMoveEvent *ev);
    void dropEvent(QDropEvent *ev);
    

    };

    #endif // STEXTBROWSER_H
    @


    @#include "PaintedTextBrowser.h"

    #include <QApplication>
    #include <QDebug>
    #include <QPainter>
    #include <QFontMetrics>
    #include <QMimeData>
    #include <QMouseEvent>
    #include <QPoint>

    #include <iostream>
    #include <math.h>

    PaintedTextBrowser::PaintedTextBrowser(QWidget *parent) : QTextBrowser(parent)
    {
    int width = this->fontMetrics().width(" ");
    lineXPos = width;
    setAcceptDrops(true);
    }

    void PaintedTextBrowser::paintEvent(QPaintEvent *ev)
    {
    QTextBrowser::paintEvent(ev);

    QPainter qp(viewport());
    qp.drawLine(lineXPos, 0, lineXPos, this->height());
    
    qDebug() << "painted";
    

    }

    void PaintedTextBrowser::mousePressEvent(QMouseEvent *ev)
    {
    if (ev->button() == Qt::LeftButton) {
    dragPos = ev->pos();
    }
    }

    void PaintedTextBrowser::mouseMoveEvent(QMouseEvent *ev)
    {
    if((ev->buttons() & Qt::LeftButton) && abs(dragPos.x() - lineXPos) <= LINE_GRAB ) {
    qDebug() << "Drag started";

        QDrag *drag = new QDrag(this);
        QMimeData *mimeData = new QMimeData();
        drag->setMimeData(mimeData);
        linePix = QPixmap(1, this->height() - 5);
        linePix.fill(Qt::black);
        drag->setPixmap(linePix);
        QPoint hotspotPoint(0, ev->pos().y());
        drag->setHotSpot(hotspotPoint);
        Qt::DropAction drop = drag->exec&#40;&#41;;
    }
    

    }

    void PaintedTextBrowser::dragEnterEvent(QDragEnterEvent *ev)
    {
    ev->accept();
    }

    void PaintedTextBrowser::dragMoveEvent(QDragMoveEvent *ev)
    {
    ev->accept();
    }

    void PaintedTextBrowser::dropEvent(QDropEvent *ev)
    {
    QPoint pos = ev->pos();
    lineXPos = pos.x();
    qDebug() << "Dropped " << lineXPos;
    ev->accept();

    this->update();
    qApp->processEvents();
    

    }
    @

    I update the widget when the drag-drop event is completed. The main logic can be explained as:

    1. In a QTextBrowser draw a vertical line.
    2. Allow dragging on it if the drag starts at a point within LINE_GRAB of the vertical line.
    3. Decide the new position of the line it drop location.
    4. In the paint event redraw the widget with new line position.

    The problem is that the updated widget is not shown unless I minimize-maximize the window or switch desktops on KDE.

    I have tried calling repaint() on the widget in dropEvent() function too.

    Please help.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.