2017-08-04 75 views
0

私はいくつかのQListWidgetを持っており、これらすべてのリストに対して1つの行を選択できるようにします。たとえば、リストの1つで行を選択した場合、他のリストの他の選択はすべて消去されます。どうすればこれを達成できますか?Qt:複数のQListWidgetsが選択され、1つのエントリのみが選択されました

ボタンのQButtonGroupに似た、組み込みの方法がありますか?そうでない場合は、自分でこれを実装しようとするときに取るべきアプローチはどのようにお勧めしますか?グレイトフル助けのためと種類に関して

、 トード

+2

他のすべてのリストウィジェットを選択解除するすべてのリストウィジェット用のシグナルハンドラを作成してください。このためのAFAIKは用意されていません。 (あなたは慎重かもしれません:選択を解除すると、選択信号も発火する可能性があるので、再帰に対する何らかのロックを考慮する必要があります)。 – Scheff

答えて

1

私の知る限りでは、何の準備はありません内蔵の複数のリストビューで、単一の選択を提供する機能。

代わりに、関連するリストビューの1つが変更されるたびに、これを行う信号ハンドラによって信号を処理することができます。QSelectionModel::selectionChanged

これにより、選択をクリアするとselectionChangedシグナルも出されると考える必要があります。 (そうでなければ、のスタックオーバーフローが発生するまで、シグナルハンドラの再帰呼び出しで終了することがあります)。

残念ながら、私はQtをC++で使用しています。 (私のPythonの知識はかなり限られている。)

したがって、私は今を提供することができるすべては、C++での私の "コンセプトの証明" です:

#include <QtWidgets> 

void singleSel(QListView *pLstView, const QList<QListView*> &pLstViews) 
{ 
    for (QListView *pLstViewI : pLstViews) { 
    if (pLstViewI == pLstView) continue; // skip sender 
    // the check is necessary to prevent recursions... 
    if (pLstView->selectionModel()->hasSelection()) { 
     // ...as this causes emission of selectionChanged() signal as well: 
     pLstViewI->selectionModel()->clearSelection(); 
    } 
    } 
} 

int main(int argc, char **argv) 
{ 
    qDebug() << "Qt Version: " << QT_VERSION_STR; 
    QApplication app(argc, argv); 
    // build contents 
    QStandardItemModel tblModel(0, 1); 
    for (int i = 0; i < 10; ++i) { 
    tblModel.appendRow(
     new QStandardItem(QString::fromUtf8("Entry %0").arg(i + 1))); 
    } 
    // build some GUI 
    QWidget win; 
    QHBoxLayout qHBox; 
    QListView lstView1; 
    lstView1.setModel(&tblModel); 
    qHBox.addWidget(&lstView1); 
    QListView lstView2; 
    lstView2.setModel(&tblModel); 
    qHBox.addWidget(&lstView2); 
    QListView lstView3; 
    lstView3.setModel(&tblModel); 
    qHBox.addWidget(&lstView3); 
    win.setLayout(&qHBox); 
    win.show(); 
    // install signal handlers 
    QList<QListView*> pLstViews = { &lstView1, &lstView2, &lstView3 }; 
    QObject::connect(lstView1.selectionModel(), 
    &QItemSelectionModel::selectionChanged, 
    [&lstView1, &pLstViews](const QItemSelection&, const QItemSelection &) 
    { 
     singleSel(&lstView1, pLstViews); 
    }); 
    QObject::connect(lstView2.selectionModel(), 
    &QItemSelectionModel::selectionChanged, 
    [&lstView2, &pLstViews](const QItemSelection&, const QItemSelection &) 
    { 
     singleSel(&lstView2, pLstViews); 
    }); 
    QObject::connect(lstView3.selectionModel(), 
    &QItemSelectionModel::selectionChanged, 
    [&lstView3, &pLstViews](const QItemSelection&, const QItemSelection &) 
    { 
     singleSel(&lstView3, pLstViews); 
    }); 
    // exec. application 
    return app.exec(); 
} 

私は(64ビット)は、Windows 10でコンパイルとテスト。これは、それがどのように見えるかです:

Snapshot of testQSingleSelectInMultipleListViews


アップデート:私は見たものから

#!/usr/bin/python3 

import sys 
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QListView 
from PyQt5.QtGui import QStandardItemModel, QStandardItem 

def singleSelect(lstView, lstViews): 
    for lstViewI in lstViews: 
    if lstViewI == lstView: 
     continue 
    # the check is necessary to prevent recursions... 
    if lstViewI.selectionModel().hasSelection(): 
     # ...as this causes emission of selectionChanged() signal as well: 
     lstViewI.selectionModel().clearSelection() 

if __name__ == '__main__': 
    app = QApplication(sys.argv) 
    # build contents 
    tblModel = QStandardItemModel(0, 1) 
    for i in range(0, 10): 
    tblModel.appendRow(QStandardItem("Entry %d" % (i + 1))) 
    # build GUI 
    win = QWidget() 
    hBox = QHBoxLayout() 
    lstView1 = QListView() 
    lstView1.setSelectionMode(QListView.SingleSelection) 
    lstView1.setModel(tblModel) 
    hBox.addWidget(lstView1) 
    lstView2 = QListView() 
    lstView2.setSelectionMode(QListView.SingleSelection) 
    lstView2.setModel(tblModel) 
    hBox.addWidget(lstView2) 
    lstView3 = QListView() 
    lstView3.setSelectionMode(QListView.SingleSelection) 
    lstView3.setModel(tblModel) 
    hBox.addWidget(lstView3) 
    win.setLayout(hBox) 
    win.show() 
    # install signal handlers 
    lstViews = [lstView1, lstView2, lstView3] 
    lstView1.selectionModel().selectionChanged.connect(lambda sel, unsel: singleSelect(lstView1, lstViews)) 
    lstView2.selectionModel().selectionChanged.connect(lambda sel, unsel: singleSelect(lstView2, lstViews)) 
    lstView3.selectionModel().selectionChanged.connect(lambda sel, unsel: singleSelect(lstView3, lstViews)) 
    # exec. application 
    sys.exit(app.exec_()) 

:私はポートへのPython/PyQt5にC++アプリケーションを試してみました

テストでは、C++で書かれたものと同じように動作します。 唯一の例外は、リストビューを変更するときにエントリを2回クリックする必要があることです。

Snapshot of testQSingleSelectInMultipleListViews.py

私はすでに言ったように:PyQtはで私の経験は非常に限られています。 (実際に、私はこのサンプルを作って今日始めました。)したがって、私は何かを監督しているかもしれません。塩の穀物でそれを取ってください...

+0

@sunyata私はPython3/PyQt5への移植を行いました。それは悪くはないように見えますが、テキストの最後に私の「免責」を考慮してください。 – Scheff