2017-02-26 11 views
3

QTableWidget 헤더에 체크 박스를 설정하는 방법. 어떻게 그것에 대한 바로 가기가 없다고 말합니다 .. QHeaderView에 그것은 체크 박스를 표시하지 않습니다 ..QtableWidget의 헤더에있는 체크 박스

QTableWidget* table = new QTableWidget(); 
QTableWidgetItem *pItem = new QTableWidgetItem("All"); 
pItem->setCheckState(Qt::Unchecked); 
table->setHorizontalHeaderItem(0, pItem); 

답변

0

Here, at Qt Wiki,을 모두 선택 확인란을 추가하면 headerView 자신을 서브 클래스해야합니다. 여기

는 위키 대답을 요약 한 것입니다 :

"현재이 헤더에 위젯을 삽입 할 API는 없지만, 당신은 헤더에 삽입하기 위해 체크 박스를 직접 그릴 수

. 당신이 할 수있는 것은, QHeaderView를 서브 클래스 paintSection() 다시 구현 한 다음이 확인란을하려는 부분에서 PE_IndicatorCheckBox와 drawPrimitive()를 호출하는 것입니다.

또한 위해, 체크 박스를 클릭 할 때 감지 할 mousePressEvent() 다시 구현해야합니다 체를 칠하기 cked 및 unchecked 상태.

#include <QtGui> 

class MyHeader : public QHeaderView 
{ 
public: 
    MyHeader(Qt::Orientation orientation, QWidget * parent = 0) : QHeaderView(orientation, parent) 
    {} 

protected: 
    void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const 
    { 
    painter->save(); 
    QHeaderView::paintSection(painter, rect, logicalIndex); 
    painter->restore(); 
    if (logicalIndex == 0) 
    { 
     QStyleOptionButton option; 
     option.rect = QRect(10,10,10,10); 
     if (isOn) 
     option.state = QStyle::State_On; 
     else 
     option.state = QStyle::State_Off; 
     this->style()->drawPrimitive(QStyle::PE_IndicatorCheckBox, &option, painter); 
    } 

    } 
    void mousePressEvent(QMouseEvent *event) 
    { 
    if (isOn) 
     isOn = false; 
    else 
     isOn = true; 
    this->update(); 
    QHeaderView::mousePressEvent(event); 
    } 
private: 
    bool isOn; 
}; 


int main(int argc, char **argv) 
{ 
    QApplication app(argc, argv); 
    QTableWidget table; 
    table.setRowCount(4); 
    table.setColumnCount(3); 

    MyHeader *myHeader = new MyHeader(Qt::Horizontal, &table); 
    table.setHorizontalHeader(myHeader); 
    table.show(); 
    return app.exec(); 
} 
+0

테이블보기에서 헤더 내에서 체크 박스를 호출하기위한 다른 방법이 없다 : 아래

의 예는이 작업을 수행 할 수있는 방법을 보여줍니다? –

+0

우리는 디자인에 우리 자신의 체크 박스를 페인트해야하고 테이블 뷰에서 헤더를 호출해야합니다. 감사합니다. 확인란을 구현하는 데이 메서드를 사용해 보겠습니다. –