在tableWidget中,我有一列完全由可選項目組成。 我無法弄清楚如何中心的複選框,或者至少去除下一文本框。 正如你可以在這張照片文本框看到了那個醜陋的外形,當我點擊單元格,我想,在整個表關掉,如果有可能。 我讀過了,我需要使用委託來控制項目/圖標的位置,但對於像我這樣的nooby來說,理解這個過程需要很長的時間,所以如果有一些簡單的解決方案可以使該列不那麼難看,請,例如,將不勝感激。對齊qTableWidget中的可檢查項目
回答
該矩形是一個焦點recangle和cannot be hidden via an stylesheet。
編輯: 所以,你必須選項:
1 - 看來,u可以使用
tablewidget->setFocusPolicy(Qt::NoFocus);
但是你將失去處理鍵盤事件的能力。 請參閱FocusPolicy
2 - 將可檢查的widgetitems設置爲禁用,無法通過setFlags進行選擇。我不知道這是否是一個錯誤,但在我的Qt我將alowed仍然點擊複選框
3 - 設置你的第一列可檢查雖然setFlags過了,只是不使用第二列。複選框將在同一列的字符串,但在左側顯示。
4-您不想創建的自定義委託。
1 - tableWidget.setFocusPolicy(QtCore.Qt.NoFocus)幫助焦點邊框(我只有鼠標事件在表中,所以TNN的) 2 - 不能用Python做。 setCheckState不起作用,我需要在某些情況下自動完成。 3 - 不能做。我有幾列複選框,我需要將它們分開。 4 - 來自示例的委託消除焦點(在第1項中解決),您能舉例說明它在哪裏用於對齊嗎? – Aleksandar 2013-04-30 09:47:50
用法:
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);
}
}
樣品:
個主題,以改善:
- 選擇繪圖
- 鼠標懸停處理
- 雙擊處理
- 鍵盤編輯事件//有很多在谷歌的例子,計算器
注意:你的模型應該處理Qt的:: CheckStateRole作用SetData方法。
我想就是這樣。沒有時間將它翻譯成python,但似乎合法。賞金是你的 – Aleksandar 2013-05-01 23:07:52
隨意問,如果你還有其他問題。 – 2013-05-02 09:44:34
一個使用pyqt4的例子。是「只勾選」實現自定義委託第二列 - 從falsinsoft
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)
你如何驗證mark/unmark chk框? – 2017-12-28 03:04:39
- 1. 替換QTableWidget中的項目
- 2. 對齊div中的項目?
- 3. 對齊HTML中的項目
- 4. 對齊表中的項目
- 5. GroupBox中的項目對齊
- 6. 檢測項目何時添加到QTableWidget
- 7. 在QTableWidget中對齊單元格中心?
- 8. QTableWidget更新項目
- 9. Android的列表查看項目對齊
- 10. QListWidget對齊項目中心
- 11. 在td中對齊項目
- 12. QListWidget對齊項目
- 13. 對齊HTML項目
- 14. CheckBoxList項目對齊
- 15. Flexbox的對齊項目
- 16. 添加QtableWidget列中的項目列表
- 17. 如何填充QTableWidget的可編輯的項目,並沒有可編輯項目
- 18. Gridview中的居中對齊項目
- 19. 對齊右側列表中的項目
- 20. 無法對齊AppBar中的項目
- 21. 對齊中心的GridView項目
- 22. 列表框中的項目對齊
- 23. 對齊堆疊面板中的項目?
- 24. 從其單元格項檢索父QTableWidget項目
- 25. 使用flexbox對齊項目
- 26. Highcharts.js傳奇項目對齊
- 27. Android TV:VerticalGridFragment行項目對齊
- 28. 對齊項目列表
- 29. 使用CSS對齊項目
- 30. 如何對齊actionscript項目?
我認爲,唯一的辦法調整。 繪製複選框並刪除選擇框非常容易。如果有必要,我可以提供C++示例。 – 2013-04-29 11:10:45
@DmitrySazonov我已經解決了選擇框架問題,但是如果您有顯示如何在特定列中使用代理複選框項目對齊方式的C++示例,請將其作爲答案發布。 – Aleksandar 2013-04-30 09:55:16
好的,在一瞬間... – 2013-04-30 13:15:48