2013-04-26 55 views
3

在tableWidget中,我有一列完全由可選項目組成。 我無法弄清楚如何中心的複選框,或者至少去除下一文本框。 正如你可以在這張照片enter image description here文本框看到了那個醜陋的外形,當我點擊單元格,我想,在整個表關掉,如果有可能。 我讀過了,我需要使用委託來控制項目/圖標的位置,但對於像我這樣的nooby來說,理解這個過程需要很長的時間,所以如果有一些簡單的解決方案可以使該列不那麼難看,請,例如,將不勝感激。對齊qTableWidget中的可檢查項目

+0

我認爲,唯一的辦法調整。 繪製複選框並刪除選擇框非常容易。如果有必要,我可以提供C++示例。 – 2013-04-29 11:10:45

+0

@DmitrySazonov我已經解決了選擇框架問題,但是如果您有顯示如何在特定列中使用代理複選框項目對齊方式的C++示例,請將其作爲答案發布。 – Aleksandar 2013-04-30 09:55:16

+0

好的,在一瞬間... – 2013-04-30 13:15:48

回答

3

該矩形是一個焦點recangle和cannot be hidden via an stylesheet

編輯: 所以,你必須選項:

1 - 看來,u可以使用

tablewidget->setFocusPolicy(Qt::NoFocus); 

但是你將失去處理鍵盤事件的能力。 請參閱FocusPolicy

2 - 將可檢查的widgetitems設置爲禁用,無法通過setFlags進行選擇。我不知道這是否是一個錯誤,但在我的Qt我將alowed仍然點擊複選框

3 - 設置你的第一列可檢查雖然setFlags過了,只是不使用第二列。複選框將在同一列的字符串,但在左側顯示。

4-您不想創建的自定義委託。

got here an example

+0

1 - tableWidget.setFocusPolicy(QtCore.Qt.NoFocus)幫助焦點邊框(我只有鼠標事件在表中,所以TNN的) 2 - 不能用Python做。 setCheckState不起作用,我需要在某些情況下自動完成。 3 - 不能做。我有幾列複選框,我需要將它們分開。 4 - 來自示例的委託消除焦點(在第1項中解決),您能舉例說明它在哪裏用於對齊嗎? – Aleksandar 2013-04-30 09:47:50

3

用法:

pView->setItemDelegateForColumn(1, new DELEGATE::CheckBoxDelegate(this)); 

CheckBoxDelegate.h

#ifndef CHECKBOXDELEGATE_H 
#define CHECKBOXDELEGATE_H 

#include <QStyledItemDelegate> 
#include <QModelIndex> 


namespace DELEGATE 
{ 

    class CheckBoxDelegate 
     : public QStyledItemDelegate 
    { 
     Q_OBJECT 

    public: 
     CheckBoxDelegate(QObject *parent); 
     ~CheckBoxDelegate(); 

     void paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const; 
     bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index); 

    private: 
     QModelIndex m_lastClickedIndex; 
    }; 

} 


#endif // CHECKBOXDELEGATE_H 

CheckBoxDelegate.cpp

#include "CheckBoxDelegate.h" 

#include <QApplication> 
#include <QStyleOptionButton> 
#include <QPainter> 
#include <QEvent> 
#include <QMouseEvent> 


namespace DELEGATE 
{ 

    CheckBoxDelegate::CheckBoxDelegate(QObject *parent) 
     : QStyledItemDelegate(parent) 
    { 
    } 

    CheckBoxDelegate::~CheckBoxDelegate() 
    { 
    } 

    void CheckBoxDelegate::paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const 
    { 
     // Setting parameters 
     Qt::CheckState state = (Qt::CheckState)index.data(Qt::CheckStateRole).toInt(); 
     QStyleOptionButton opt; 

     opt.state = QStyle::State_Enabled; // CheckBox enabled 
     if (option.state & QStyle::State_MouseOver) 
      opt.state |= QStyle::State_MouseOver; // Mouse over sell 
     switch (state) // Check box state 
     { 
     case Qt::Unchecked: 
      opt.state |= QStyle::State_Off; 
      break; 
     case Qt::PartiallyChecked: 
      opt.state |= QStyle::State_NoChange; 
      break; 
     case Qt::Checked: 
      opt.state |= QStyle::State_On; 
      break; 
     } 
     // Check box rect 
     opt.rect = QApplication::style()->subElementRect(QStyle::SE_CheckBoxIndicator, &opt, NULL); 
     const int x = option.rect.center().x() - opt.rect.width()/2; 
     const int y = option.rect.center().y() - opt.rect.height()/2; 
     opt.rect.moveTo(x, y); 

     // Optional: draw hover focus 
     if (option.state & QStyle::State_MouseOver) 
      painter->fillRect(option.rect, QBrush(QColor(0xff, 0xff, 0xaa, 0x60))); 

     // Mandatory: drawing check box 
     QApplication::style()->drawControl(QStyle::CE_CheckBox, &opt, painter); 
    } 

    bool CheckBoxDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) 
    { 
     switch (event->type()) 
     { 
     case QEvent::MouseButtonPress: 
      m_lastClickedIndex = index; 
      break; 

     case QEvent::MouseButtonRelease: 
      { 
       if (index != m_lastClickedIndex) 
        break; 
       QMouseEvent *e = static_cast< QMouseEvent * >(event); 
       if (e->button() != Qt::LeftButton) 
        break; 
       m_lastClickedIndex = QModelIndex(); 

       QStyleOptionButton opt; 
       opt.rect = QApplication::style()->subElementRect(QStyle::SE_CheckBoxIndicator, &opt, NULL); 
       const int x = option.rect.center().x() - opt.rect.width()/2; 
       const int y = option.rect.center().y() - opt.rect.height()/2; 
       opt.rect.moveTo(x, y); 

       if (opt.rect.contains(e->pos())) 
       { 
        // TODO: process click on checkbox logic 
        Qt::CheckState state = (Qt::CheckState)index.data(Qt::CheckStateRole).toInt(); 
        switch (state) 
        { 
        case Qt::Unchecked: 
         state = Qt::PartiallyChecked; 
         break; 
        case Qt::PartiallyChecked: 
         state = Qt::Checked; 
         break; 
        case Qt::Checked: 
         state = Qt::Unchecked; 
         break; 
        } 

        model->setData(index, state, Qt::CheckStateRole); 
       } 
       return true; 
      } 

     default: 
      break; 
     } 

     return QAbstractItemDelegate::editorEvent(event, model, option, index); 
    } 

} 

樣品:

sample

主題,以改善:

  • 選擇繪圖
  • 鼠標懸停處理
  • 雙擊處理
  • 鍵盤編輯事件//有很多在谷歌的例子,計算器

注意:你的模型應該處理Qt的:: CheckStateRole作用SetData方法。

+0

我想就是這樣。沒有時間將它翻譯成python,但似乎合法。賞金是你的 – Aleksandar 2013-05-01 23:07:52

+0

隨意問,如果你還有其他問題。 – 2013-05-02 09:44:34

5

一個使用pyqt4的例子。是「只勾選」實現自定義委託第二列 - 從falsinsoft

qcheck box centre inside pyqt4 atble widget

table = QTableWidget() 
cell_widget = QWidget() 
chk_bx = QCheckBox() 
chk_bx.setCheckState(Qt.Checked) 
lay_out = QHBoxLayout(cell_widget) 
lay_out.addWidget(chk_bx) 
lay_out.setAlignment(Qt.AlignCenter) 
lay_out.setContentsMargins(0,0,0,0) 
cell_widget.setLayout(lay_out) 
tableWidget.setCellWidget(i, 0, cell_widget) 
+0

你如何驗證mark/unmark chk框? – 2017-12-28 03:04:39