对齐qTableWidget中的可检查项目

Ale*_*dar 5 python qt qt4 pyqt4 qtablewidget

在tableWidget中,我有一列完全由可检查项组成.我无法弄清楚如何使复选框居中或至少删除旁边的文本框.正如你在这张照片上看到的那样在此输入图像描述当我点击单元格时,文本框有那个丑陋的轮廓,如果有可能,我希望在整个表格中关闭它.我已经读过我需要使用代理人来控制项目/图标的位置,但对于像我这样的人来说理解这一点需要很长时间,所以如果有一些简单的解决方案可以使该列不那么难看,请举例,我们将不胜感激.

Eri*_*ick 8

使用pyqt4工作的一个例子.改编自falsinsoft

qqck框中心内部pyqt4 atble小部件

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)
Run Code Online (Sandbox Code Playgroud)


Dmi*_*nov 6

用法:

pView->setItemDelegateForColumn( 1, new DELEGATE::CheckBoxDelegate( this ) );
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

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 );
    }

}
Run Code Online (Sandbox Code Playgroud)

样本:

样本

需要改进的科目:

  • 选型图
  • 鼠标悬停处理
  • 双击处理
  • 键盘编辑事件 // google、stackoverflow 中有很多例子

注意:您的模型应该处理 Qt::CheckStateRole 角色的 setData 方法。


tro*_*mpa 5

该矩形是焦点调整,不能通过样式表隐藏

Edit: So you have four options:

1 - It seems that u can use

 tablewidget->setFocusPolicy(Qt::NoFocus);
Run Code Online (Sandbox Code Playgroud)

But you will lose the ability to process keyboard events. See FocusPolicy

2 - Set the checkable widgetitems as disabled, not selectable through setFlags. I don't know if this is a bug, but in my Qt i will be alowed to still click on checkboxes

3 - Set your first column as checkable though setFlags too, and just dont use that second column. Checkboxes will be shown in same column that the Strings but at left.

4- That custom delegate you dont want to create.

And you got here an example