·¢²¼Ê±¼ä : ÐÇÆÚÈý ÎÄÕÂQt ModelView ѧϰ±Ê¼Ç¸üÐÂÍê±Ï¿ªÊ¼ÔĶÁ2545926cda38376baf1faec0
QItemSelectionModel *selectionModel = table->selectionModel(); QModelIndex topLeft; QModelIndex bottomRight;
topLeft = model->index(0, 0, QModelIndex()); bottomRight = model->index(5, 2, QModelIndex());
QItemSelection selection(topLeft, bottomRight);
selectionModel->select(selection, QItemSelectionModel::Select); ½á¹ûÈçÏ£º
¶ÁÈ¡Ñ¡Ôñ״̬
´æ´¢ÔÚÑ¡ÔñÄ£ÐÍÖÐindexes¿ÉÒÔÓÃselectionIndexes()º¯ÊýÀ´¶ÁÈ¡¡£Ëü·µ»ØÒ»¸öδÅÅÐòµÄmodel indexesÁÐ±í£¬ÎÒÃÇ¿ÉÒÔ±éÀúËü£¬Èç¹ûÎÒÃÇÖªµÀËûÃǹØÁªÓÚÄĸömodelµÄ»°¡£
QModelIndexList indexes = selectionModel->selectedIndexes(); QModelIndex index; foreach(index, indexes) {
QString text = QString(\ model->setData(index, text); }
Ñ¡ÔñÄ£ÐÍÔÚÑ¡Ôñ·¢Éú±ä»¯Ê±»á·¢³öÐźš£ÕâÓÃÓÚ֪ͨ±ðµÄ×é¼þ°üÀ¨ÕûÌåÓ뵱ǰ½¹µãÏîËù·¢ÉúµÄ±ä»¯¡£ÎÒÃÇ¿ÉÒÔÁ¬½ÓselectionChanged()Ðźŵ½Ò»¸ö²Û£¬¼ì²éµ±ÐźŲúÉúʱÄÄЩÏî
±»Ñ¡Ôñ»ò±»È¡ÏûÑ¡Ôñ¡£Õâ¸ö²Û±»µ÷ÓÃʱ´øÓÐÁ½¸ö²ÎÊý£¬ËüÃǶ¼ÊÇQItemSelection¶ÔÏó£¬Ò»¸ö°üº¬Ð±»Ñ¡ÔñµÄÏÁíÒ»¸ö°üº¬Ð½ü±»È¡ÏûÑ¡ÔñµÄÏî¡£ÏÂÃæµÄ´úÂëÑÝʾÁ˸øÐÂÑ¡ÔñµÄÏîÌí¼ÓÊý¾ÝÄÚÈÝ£¬Ð½ü±»È¡ÏûÑ¡ÔñµÄÏîµÄÄÚÈݱ»Çå¿Õ¡£
void MainWindow::updateSelection(const QItemSelection &selected, const QItemSelection &deselected) {
QModelIndex index;
QModelIndexList items = selected.indexes(); foreach (index, items) {
QString text = QString(\ model->setData(index, text); }
items = deselected.indexes(); foreach (index, items) model->setData(index, \ }
Ò²¿ÉÒÔͨ¹ýÏìÓ¦currentChanged()ÐźÅÀ´¸ú×Ùµ±Ç°½¹µãÏî.¶ÔÓ¦µÄ²Û¾ÍÓÐÁ½¸ö½ÓÊÕ²ÎÊý£¬Ò»¸ö±íʾ֮ǰµÄ½¹µã£¬ÁíÒ»¸ö±íʾµ±Ç°µÄ½¹µã¡£
void MainWindow::changeCurrent(const QModelIndex ¤t, const QModelIndex &previous) {
statusBar()->showMessage(
tr(\ .arg(previous.row()).arg(previous.column()) .arg(current.row()).arg(current.column())); } ¸üÐÂÑ¡Ôñ
Ñ¡ÔñÖ¸ÁîÊÇͨ¹ýÑ¡Ôñ±êÖ¾ÌṩµÄ£¬Ëü±»¶¨ÒåÔÚQItemSelectionModel::SelectionFlagÖС£³£ÓõÄÓÐSelect±ê¼Ç£¬Toggle±ê¼Ç£¬Deselect±ê¼Ç,Current±ê¼Ç£¬Clear±ê¼Ç£¬ÆäÒâÒåһĿÁËÈ»¡£ÑØÉÏÃæÀý×ӵĽá¹ûÖ´ÐÐÒÔÏ´úÂ룺
QItemSelection toggleSelection;
topLeft = model->index(2, 1, QModelIndex()); bottomRight = model->index(7, 3, QModelIndex()); toggleSelection.select(topLeft, bottomRight);
selectionModel->select(toggleSelection, QItemSelectionModel::Toggle); ½á¹ûÈçÏÂ:
ȱʡÇé¿öÏ£¬Ñ¡ÔñÖ¸ÁîÖ»Õë¶Ôµ¥¸öÏî(ÓÉmodel indexesÖ¸¶¨)¡£È»¶ø£¬Ñ¡ÔñÖ¸Áî¿ÉÒÔͨ¹ýÓëÁíÍâ±ê¼ÇµÄ½áºÏÀ´¸Ä±äÕûÐкÍÕûÁС£¾ÙÀýÀ´Ëµ£¬¼ÙÈçÄãֻʹÓÃÒ»¸öindexÀ´µ÷ÓÃselect(),µ«ÊÇÓÃSelect±ê¼ÇÓëRows±ê¼ÇµÄ×éºÏ£¬ÄÇô°üÀ¨ÄǸöÏîµÄÕûÐж¼½«±»Ñ¡Ôñ¡£¿´ÒÔÏÂʾÀý£º
QItemSelection columnSelection;
topLeft = model->index(0, 1, QModelIndex()); bottomRight = model->index(0, 2, QModelIndex()); columnSelection.select(topLeft, bottomRight); selectionModel->select(columnSelection,
QItemSelectionModel::Select | QItemSelectionModel::Columns); QItemSelection rowSelection;
topLeft = model->index(0, 0, QModelIndex());
bottomRight = model->index(1, 0, QModelIndex()); rowSelection.select(topLeft, bottomRight); selectionModel->select(rowSelection,
QItemSelectionModel::Select | QItemSelectionModel::Rows); ½á¹ûÈçÏÂ
Ñ¡ÔñÄ£ÐÍÖÐËùÓÐÏî
ΪÁËÑ¡ÔñmodelÖеÄËùÓÐÏ±ØÐëÏȵô´½¨Ò»¸öÑ¡Ôñ£¬Ëü°üÀ¨µ±Ç°²ã´ÎÉϵÄËùÓÐÏî: QModelIndex topLeft = model->index(0, 0, parent);
QModelIndex bottomRight = model->index(model->rowCount(parent)-1, model->columnCount(parent)-1, parent); QItemSelection selection(topLeft, bottomRight);
selectionModel->select(selection, QItemSelectionModel::Select); ¶¥¼¶index¿ÉÒÔÕâÑù£º
QModelIndex parent = QModelIndex();
¶Ô¾ßÓвã´Î½á¹¹µÄmodelÀ´Ëµ£¬¿ÉÒÔʹÓÃhasChildren()º¯ÊýÀ´¾ö¶¨¸ø¶¨ÏîÊÇ·ñÊÇÆäËüÏîµÄ¸¸Ïî¡£
Qt Model/View ѧϰ±Ê¼Ç (Æß) ÇåÔ´ÓÎÃñ gameogre@gmail.com