开发者

How to get the Selected Row Number from QTableView?

开发者 https://www.devze.com 2023-01-19 01:46 出处:网络
I am new to QT. i am using FrozenTabelWidget it\'s derived from the QTableView. How to get the selected row number. if the index changed(user click any cell) then I need to get that Cell Row Number?

I am new to QT. i am using FrozenTabelWidget it's derived from the QTableView. How to get the selected row number. if the index changed(user click any cell) then I need to get that Cell Row Number?

My code is below:

//freezetablewidget.cpp

#include <QtGui>
#include "freezetablewidget.h"

 FreezeTableWidget::FreezeTableWidget(QAbstractItemModel * model)
 {
       setModel(model);
       frozenTableView = new QTableView(this);
       init();
       connect(horizontalHeader(),SIGNAL(sectionResized ( int ,int,int )), this,
               SLOT(updateSectionWidth(int, int, int)));
       connect(frozenTableView->verticalScrollBar(), SIGNAL(valueChanged(int)),
               verticalScrollBar(), SLOT(setValue(int)));
       connect(verticalScrollBar(), SIGNAL(valueChanged(int)),
               frozenTableView->verticalScrollBar(), SLOT(setValue(int)));
 }

 FreezeTableWidget::~FreezeTableWidget()
 {
       delete frozenTableView;
 }

 void FreezeTableWidget::init()
 {
       frozenTableView->hideColumn(0);
       frozenTableView->setModel(model());
       frozenTableView->setFocusPolicy(Qt::NoFocus);
       frozenTableView->verticalHeader()->hide();
       frozenTableView->horizontalHeader()->setResizeMode(QHeaderView::Fixed);
       viewport()->stackUnder(frozenTableView);
       frozenTableView->setSelectionBehavior(QAbstractItemView::SelectRows);
       frozenTableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
       frozenTableView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
       frozenTableView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
       frozenTableView->show();
       updateFrozenTableGeometry();
       setHorizontalScrollMode(ScrollPerPixel);
       setVerticalScrollMode(ScrollPerPixel);
       frozenTableView->setVerticalScrollMode(ScrollPerPixel);
 }

 void FreezeTableWidget::currentChanged(const QModelIndex &current, const QModelIndex &previous)
 {
    QMessageBox::about(this,"Test","Hello");
 }

 void FreezeTableWidget::selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
 {
    QMessageBox::about(this,"Test","123456");
 }


 void FreezeTableWidget::updateSectionWidth(int logicalIndex, int, int newSize)
 {
       if(logicalIndex==0){
             frozenTableView->setColumnWidth(0,newSize);
             updateFrozenTableGeometry();
       }
 }

 void FreezeTableWidget::updateSectionHeight(int logicalIndex, int, int newSize)
 {
      frozenTableView->setRowHeight(logicalIndex, newSize);
 }

 void FreezeTableWidget::resizeEvent(QResizeEvent * event)
 {
       QTableView::resizeEvent(event);
       updateFrozenTableGeometry();
  }

 QModelIndex FreezeTableWidget::moveCursor(CursorAction cursorAction,
                                           Qt::KeyboardModifiers modifiers)
 {
       QModelIndex current = QTableView::moveCursor(cursorAction, modifiers);

       if(cursorAction == MoveLeft && current.column()>0
          && visualRect(current).topLeft().x() < frozenTableView->columnWidth(0) ) {

             const int newValue = horizontalScrollBar()->value() + visualRect(current).topLeft().x()
                                  - frozenTableView->columnWidth(0);
             horizontalScrollBar()->setValue(newValue);
       }
       return current;
 }

 void FreezeTableWidget::updateFrozenTableGeometry()
 {
       frozenTableView->setGeometry(frameWidth(),
                                     frameWidth(), this->width(),
                                     viewport()->height()+horizontalHeader()->height());
 }

 //freezetablewidget.h

 #ifndef FREEZETABLEWIDGET_H
 #define FREEZETABLEWIDGET_H
 #include <QTableView>

 class FreezeTableWidget : public QTableView {
      Q_OBJECT

 public:
       FreezeTableWidget(QAbstractItemModel * model);
       ~FreezeTableWidget();

 protected:
       virtual void resizeEvent(QResizeEvent *event);
       virtual QModelIndex moveCursor(CursorAction cursorAction, Qt::KeyboardModifiers modifiers);
       void currentChanged(const QModelIndex &current, const QModelIndex &previous);
       void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected);

 signals:

       void indexChanged();

 private:
       QTableView *frozenTableView;
       void init();
       void updateFrozenTableGeometry();

 private slots:
       void updateSectionWidth(int logicalIndex,int, int newSize);
       void updateSectionHeight(int logicalIndex, int, int newSize);

 };
 #endif

 // Main .cpp
 #include <QApplication>
#include <QStandardItemModel>
#include <QFile>
#include <QDir>
#include <QDebug>
#include "freezetablewidget.h"

int main( int argc, char** argv )
{

    QApplication app( argc, argv );
    QStandardItemModel *model=new QStandardItemModel();

    QString line;
    QStringList list;

    line = "#,Name,Score,Sub1,Sub2,Sub4,Sub5,Sub6";
    list= line.simplified().split(",");
    model->setHorizontalHeaderLabels(list);

    int row=0;
    line="aaa,bbb,ccc,ddd";
    list= line.simplified().split(",");

    foreach(QString str,list) {
        model->setItem(row ,0, new QStandardItem(QString::number(row + 1)));
        model->setItem(row ,1, new QStandardItem(str));
        model->setItem(row ,2, new QStandardItem("600"));
        model->setItem(row ,3, new QStandardItem("5"));
        model->setItem(row ,4, new QStandardItem("60"));
        model->setItem(row ,5, new QStandardItem("100"));
        model->setItem(row ,6, new QStandardItem("100"));
        model->setItem(row ,7, new QStandardItem("NA"));
    row++;
    }

    FreezeTableWidget *tableView = new FreezeTableWidget(model);

    t开发者_如何学CableView->setWindowTitle(QObject::tr("Frozen Column Example"));

     tableView->resize(900,200);
    tableView->show();
    return app.exec();
}

Now if I click any cell I try to display QMessageBox, but it's not working. Please Help me to fix the problem.


I'm not exactly sure what you're trying to do with the FreezeTableWidget.

The basic problem is that you're creating two tables when you create a FreezeTableWidget, both of which are in the same space with one on top of the other. That is you've got a QTableView (frozenTableView) placed on top of a FreezeTableWidget (this).

That means the selection changes are happening in frozenTableView, which is a QTableView, and thus your derived FreezeTableWidget won't get any method invocations from.

If you want to see this, in ::init() replace the line:

frozenTableView->show();

with

frozenTableView->hide();

I can't see a reason why you need two tables on top of each other. If this wasn't what you were trying to do, and you only want a single table view (which I recommend, unless you have reasons you need two of the same tables on top of each other) you can delete the frozenTableView member variable. You can then replace most of the code that refers to frozenTableView (for example, most of the code in ::init that sets up the behavior you want) with this.

I've included some sample code below:

// FreezeTableWidget.cpp
#include <QtGui>
#include "freezetablewidget.h"

 FreezeTableWidget::FreezeTableWidget(QAbstractItemModel * model)
 {
       setModel(model);
       init();
       connect(horizontalHeader(),SIGNAL(sectionResized ( int ,int,int )), this,
               SLOT(updateSectionWidth(int, int, int)));
 }

 FreezeTableWidget::~FreezeTableWidget()
 {
 }

 void FreezeTableWidget::init()
 {
       this->hideColumn(0);
       this->setModel(model());
       this->setFocusPolicy(Qt::NoFocus);
       this->verticalHeader()->hide();
       this->horizontalHeader()->setResizeMode(QHeaderView::Fixed);
       this->setSelectionBehavior(QAbstractItemView::SelectRows);
       this->setEditTriggers(QAbstractItemView::NoEditTriggers);
       this->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
       this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
       setHorizontalScrollMode(ScrollPerPixel);
       setVerticalScrollMode(ScrollPerPixel);
 }


Override QTableView::currentChanged(). The row number is then available with row() from the QModelIndex object passed to currentChanged().

You may also want to override selectionChanged(). From there you can get a QList of QModelIndex objects through QItemSelection::indexes().

0

精彩评论

暂无评论...
验证码 换一张
取 消