Adding custom style to button added in QTableview

ε祈祈猫儿з 提交于 2020-01-07 04:58:43

问题


I have created one table by using QTableview and QAbstractTableModel. In the last column, I am also able to add a button for every row (right corner of that cell). Now I want to customize its style, like change the background color to black, border, etc.

Is there any way to achieve this?


回答1:


delegate.h:

class MyDelegate : public QItemDelegate {
    Q_OBJECT

public:
    MyDelegate(QObject *parent = 0);
    void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
    bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index); };

delegate.cpp:

 #include <QtGui>  #include "delegate.h"

 MyDelegate::MyDelegate(QObject *parent)
     : QItemDelegate(parent)  {  }


 void MyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const  {
     QStyleOptionButton button;
     QRect r = option.rect;//getting the rect of the cell
     int x,y,w,h;
     x = r.left() + r.width() - 30;//the X coordinate
     y = r.top();//the Y coordinate
     w = 30;//button width
     h = 30;//button height
     button.rect = QRect(x,y,w,h);
     button.text = "=^.^=";
     button.state = QStyle::State_Enabled;

     QApplication::style()->drawControl( QStyle::CE_PushButton, &button, painter);  }

 bool MyDelegate::editorEvent(QEvent *event, QAbstractItemModel
*model, const QStyleOptionViewItem &option, const QModelIndex &index)  {
     if( event->type() == QEvent::MouseButtonRelease )
     {
         QMouseEvent * e = (QMouseEvent *)event;
         int clickX = e->x();
         int clickY = e->y();

         QRect r = option.rect;//getting the rect of the cell
         int x,y,w,h;
         x = r.left() + r.width() - 30;//the X coordinate
         y = r.top();//the Y coordinate
         w = 30;//button width
         h = 30;//button height

         if( clickX > x && clickX < x + w )
             if( clickY > y && clickY < y + h )
             {
                 QDialog * d = new QDialog();
                 d->setGeometry(0,0,100,100);
                 d->show();
             }
     }  }

main.cpp

#include "delegate.h"

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);

    QStandardItemModel model(4, 2);
    QTableView tableView;
    tableView.setModel(&model);

    MyDelegate delegate;
    tableView.setItemDelegate(&delegate);

    tableView.horizontalHeader()->setStretchLastSection(true);
    tableView.show();
    return app.exec(); }



回答2:


IMO the best approach is to use a style sheet. http://doc.qt.io/qt-5/stylesheet-syntax.html http://doc.qt.io/qt-5/stylesheet-reference.html

qApp->setStylSheet("QTableview QPushButton {" // apply only on push buttons inside a table view
    "    background-color: red;"
    "    border-style: outset;"
    "    border-width: 2px;"
    "    border-color: beige;"
    "}");


来源:https://stackoverflow.com/questions/45812500/adding-custom-style-to-button-added-in-qtableview

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!