Paint QTableView with rounded corners.



  • Hello.
    I want to have a QTableView widget which will have a rounded corners. for that I create a class MyTableView which inherits QTableView and reimplement the paintEvent method. I do:

    void MyTableView::paintEvent(QPaintEvent *event)
    {
    QTableView::paintEvent(event);

    QPainter painter(this);

    int radius = 6;

    painter.setRenderHint(QPainter::Antialiasing);

    QPen pen = painter.pen();

    painter.setPen(Qt::NoPen);

    painter.drawRoundedRect(rect().adjusted(1, 1, -1, -1), radius, radius,
    Qt::AbsoluteSize);
    painter.drawRoundedRect(rect().adjusted(1, 1, -1, -rect().height() + 10), 2, 2,
    Qt::AbsoluteSize);

    
    and I have:
    [03.11.16 10:10:31.540] QPainter::begin: Widget painting can only begin as a result of a paintEvent
    [03.11.16 10:10:31.540] QPainter::setRenderHint: Painter must be active to set rendering hints
    [03.11.16 10:10:31.540] QPainter::pen: Painter not active
    [03.11.16 10:10:31.540] QPainter::setPen: Painter not active
    [03.11.16 10:10:31.540] QPainter::setBrush: Painter not active
    [03.11.16 10:10:31.540] QPainter::setPen: Painter not active
    [03.11.16 10:10:31.544] QPainter::begin: Widget painting can only begin as a result of a paintEvent
    [03.11.16 10:10:31.545] QPainter::setRenderHint: Painter must be active to set rendering hints
    [03.11.16 10:10:31.545] QPainter::pen: Painter not active
    
    what am I doing wrong? thank you.


  • @tokafr

    You can use: QPainter painter(this->viewport());



  • Ok I found the answer. I added
    setAttribute(Qt::WA_PaintOutsidePaintEvent, true);
    flag to the widget I am repainting



  • This post is deleted!

Log in to reply
 

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