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);
}
줄 수있는 방법을 알려주시겠습니까? 어떤 도움을 주셔서 감사합니다.
나를 위해 작동합니다. 도와 주셔서 감사합니다. –