2014-01-28 8 views
3

Qtableview 및 QSqlQueryModel을 사용하여 표시하는 Sqlite 데이터베이스에 테이블이 있습니다. 첫 번째 열에는 확인란이있는 머리글이 있어야하며 열의 모든 항목도 확인란이어야합니다. 첫 번째 열 머리글을 확인란으로 구현했으며 완벽하게 작동합니다.QTableView의 확인란 전용 열

열의 확인란이 가운데에 있어야하므로 대리인을 사용하여 페인트했습니다. 다음 코드를 사용하여 확인란을 그렸습니다. 그러나 검사하거나 선택을 취소 할 수 없습니다. 나는 그것을 구현하는 방법을 모른다.

static QRect CheckBoxRect(const QStyleOptionViewItem &view_item_style_options) { 
    QStyleOptionButton check_box_style_option; 
    QRect check_box_rect = QApplication::style()->subElementRect(
    QStyle::SE_CheckBoxIndicator, 
    &check_box_style_option); 
    QPoint check_box_point(view_item_style_options.rect.x() + 
        view_item_style_options.rect.width()/2 - 
        check_box_rect.width()/2, 
        view_item_style_options.rect.y() + 
        view_item_style_options.rect.height()/2 - 
        check_box_rect.height()/2); 
    return QRect(check_box_point, check_box_rect.size()); 
} 


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

} 

void CheckBoxDelegate::paint(QPainter *painter, 
         const QStyleOptionViewItem &option, 
         const QModelIndex &index) const { 
    bool checked = index.model()->data(index, Qt::DisplayRole).toBool(); 

    QStyleOptionButton check_box_style_option; 
    check_box_style_option.state |= QStyle::State_Enabled; 
    if (checked) { 
    check_box_style_option.state |= QStyle::State_On; 
    } else { 
    check_box_style_option.state |= QStyle::State_Off; 
    } 
    check_box_style_option.rect = CheckBoxRect(option); 

    QApplication::style()->drawControl(QStyle::CE_CheckBox, 
           &check_box_style_option, 
           painter); 
} 

다음 코드

내가 데이터베이스에서 테이블을로드 할 수 QTableView에 대한 QSqlQueryModel를 사용하는 방법을 보여줍니다.

//Load the tableview with the database table 
QSqlQueryModel model = new QSqlQueryModel(); 

//Initializaton of the query 
QSqlQuery *query = new QSqlQuery(dbm->db); 

query->prepare("SELECT * FROM UserData"); 

if(query->exec()) 
{ 
    model->setQuery(*query); 
    ui->tableView->setModel(model); 

    //The header delegate to paint a checkbox on the header 
    HeaderDelegate *myHeader = new HeaderDelegate(Qt::Horizontal, ui->tableView); 
    ui->tableView->setHorizontalHeader(myHeader); 

    int RowCount = model->rowCount(); 

    qDebug() << RowCount; 

    CheckBoxDelegate *myCheckBoxDelegate = new CheckBoxDelegate(); 

    ui->tableView->setItemDelegateForColumn(0,myCheckBoxDelegate); 

    ui->tableView->horizontalHeader()->setClickable(true); 

    ui->tableView->setSortingEnabled(true); 
} 

줄 수있는 방법을 알려주시겠습니까? 어떤 도움을 주셔서 감사합니다.

답변

0

확인 가능한 항목을 표시하는 가장 쉬운 방법은 QStandardItem을 검사 가능으로 설정할 수 있으므로 QStandardItemModel을 사용하는 것입니다. 프로토 타이핑에 좋습니다. 그러나 단점은 수동으로 모델을 채워야한다는 것입니다.

4

대리인이나 그런 것을 사용하지 않는이 솔루션을 발견했습니다. 확인란을 선택하는 데는 여전히 문제가 있습니다. 그건 너에게 달렸어.

yourSqlQueryModel = new QSqlQueryModel(); 
yourTableView = new QtableView(); 
     yourSqlQueryModel ->setQuery(yourQuery); 
     yourSqlQueryModel ->insertColumn(0);//Insert column for checkboxes 
     ui->yourTableView ->setModel(yourSqlQueryModel); 
     ui->yourTableView ->resizeColumnsToContents(); 

     int p; 
     for(p = 0;p<yourSqlQueryModel ->rowCount();p++) 
     { 
      ui->yourTableView ->setIndexWidget(yourSqlQueryModel ->index(p,0),new QCheckBox()); 
     } 

는,주의 깊게 읽고 가장 중요 여기에 당신이 생성 된 컬럼에 위젯을 삽입 할 수 있습니다 setIndexWidget 방법, 제발 :

다음 코드는 체크 박스로 채워진 컬럼을 만들 것입니다.

+0

나를 위해 작동합니다. 도와 주셔서 감사합니다. –