Hi, 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 ¤t, 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 ¤t, 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);
tableView->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.