How can I set round corners to QListVIew items?



  • Hello all!

    I have the QListView with items. when I select the item the item gets highlighted but it has ordinary corners, I want to round the corners how can I so it?

    thank you again!


  • Moderators

    use stylesheets or a custom item delegate and override it's paint method.

    Example stylesheet (untested):
    @
    listView->setStyleSheet(
    "QListView::item:selected {"
    "border: 1px solid #6a6ea9;"
    "border-radius: 3px;"
    "}"
    );
    @



  • doesn't do anything :(


  • Moderators

    just tested...works for me.

    !http://oi62.tinypic.com/2gtp1yu.jpg(screenshot)!

    @
    QListWidget l;
    l.addItems( QStringList() << "111" << "222" << "333" );
    l.setStyleSheet(
    "QListView::item:selected {"
    "border: 1px solid #6a6ea9;"
    "border-radius: 5px;"
    "color: black;"
    "}"
    );
    l.show();
    @



  • Check your syntax again. It works for me
    @MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    {
    lv = new QListView(this);
    QStandardItemModel *model = new QStandardItemModel(this);
    QStringList headers;
    model->setHorizontalHeaderLabels(headers);
    model->setRowCount(3);
    QStandardItem *one = new QStandardItem("ONE");
    QStandardItem *two = new QStandardItem("TWO");
    QStandardItem *three = new QStandardItem("THREE");

    model->setItem(0,one);
    model->setItem(1,two);
    model->setItem(2,three);
    
    
    lv->setStyleSheet("QListView::item:selected {background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 #004400, stop: 1 #00ff00); border: 1px solid #990033; border-radius: 6px;}");
    
    lv->setModel(model);
    setCentralWidget(lv);
    

    }@



    • raven-worx*
      there you have QListWidget, Does it work for QListView also because here with me it doesn't do that

  • Moderators

    [quote author="tokafr" date="1422478813"]* raven-worx*
    there you have QListWidget, Does it work for QListView also because here with me it doesn't do that [/quote]

    QListWidget derives from QListView, so yes it does work also with it.
    To be more specific the item selector works for almost all QAbstractItemView widgets.

    Then post some code, maybe we can spot something.



  • @
    listWidget = new QListView(this);

    listWidget->setSelectionBehavior(QListView::SelectRows);
    listWidget->setEditTriggers(QListView::AllEditTriggers);
    
    
    /* here we set the initial data by hand. */
    
    Connection conn1("toka","cloud","http","linux","192.168.1.108");
    Connection conn2(QObject::tr("localhost"),QObject::tr("Server"),QObject::tr("ssh"),
                         QObject::tr("windows"),QObject::tr("192.168.1.254"),QPixmap(":/icons/images.png"));
    
    model->addRow(conn1);
    model->addRow(conn2);
    proxy->setSourceModel(model);
    listWidget->setModel(proxy);
    listWidget->setStyleSheet(
          "QListView::item:selected {"
          "border: 1px solid black;"
          "border-radius: 3px;"
          "color: black;"
          "}"
          );
    listWidget->setItemDelegate(delegate);
    

    @


  • Moderators

    @listWidget->setItemDelegate(delegate);@
    Whats the type of the delegate and what it's purpose?

    Make sure it's of the type QStyledItemDelegate and of course don't override it's paint method. If you have to override the paint() method you also need to make sure that you paint the rounded border.



  • yes I paint the items, how can I paint rounded border when (the rect there is option.rect) and then fill the rounded border?
    @
    painter->drawRoundRect(option.rect);
    painter->fillRect(option.rect, option.palette.highlight());
    @

    fills the default rectangle without rounded borders


  • Moderators

    Well you never said anything about it. Don't you think this would have been of any interest from the beginning?!!

    drawRoundRect() is not a method of QPainter?!

    "drawRoundedRect()":http://doc.qt.io/qt-5/qpainter.html#drawRoundedRect is and also has parameters to specify the border radius.



  • Ok, I did it, thank you!! :)


Log in to reply
 

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