#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
MainWindow w;
w.show();
return a.exec();
}
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
file: mainwindow.cpp
#include "mainwindow.h"
MainWindow
::MainWindow(QWidget *parent
){
table->setModel(model);
table->installEventFilter(this);
//populate model with data...
for(int i=0;i<5; i++)
{
for(int j=0; j<2; j++)
{
model->setItem(i,j,item);
}
}
table->resizeRowsToContents();
table->resizeColumnsToContents();
table->setFixedSize(300,400);
vbl->addWidget(table,1);
setLayout(vbl);
setFixedSize(800,600);
}
{
if(o
== table
&& e
->type
() == QEvent::KeyPress) {
//key was pressed on param detail selection...
int row = table->currentIndex().row();
int col = table->currentIndex().column();
int rowCnt = model->rowCount()-1;
int colCnt = model->columnCount()-1;
QKeyEvent *keyEv
= static_cast <QKeyEvent
*>
(e
);
if(keyEv->key() == Qt::Key_Left)
{
qDebug()<<"Key LEFT";
if(col != 0)
emit updateFromArrow(model->index(row,col-1));
else
emit updateFromArrow(model->index(row,col));
}//end if key left
else if(keyEv->key() == Qt::Key_Right)
{
if(col != colCnt)
emit updateFromArrow(model->index(row,col+1));
else
emit updateFromArrow(model->index(row,col));
}//end keyright
else if(keyEv->key() == Qt::Key_Up)
{
if(row != 0)
emit updateFromArrow(model->index(row-1,col));
else
emit updateFromArrow(model->index(row,col));
}//end if key up
else if(keyEv->key() == Qt::Key_Down)
{
if(row != rowCnt)
emit updateFromArrow(model->index(row+1,col));
else
emit updateFromArrow(model->index(row,col));
}//end if key down
}
return false;
}
{
qDebug()<<"tree Clicked: row:"<<mi.row()<<"col:"<<mi.column()<<"curindxCol:"<<table->currentIndex().column();
if(mi.column() == 0) //if we are in first column, then jump to 2nd column <-works on clicking, not on keypress from eventFilter
{
qDebug()<<"Changing index, in col 0 for param";
m = model->index(mi.row(),1);
//table->selectionModel()->select(table->model()->index(mi.row(),1), QItemSelectionModel::ClearAndSelect);
table->setCurrentIndex(m);
}
}
MainWindow::~MainWindow()
{
}
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
table = new QTableView(this);
model = new QStandardItemModel(this);
table->setModel(model);
table->installEventFilter(this);
//populate model with data...
for(int i=0;i<5; i++)
{
for(int j=0; j<2; j++)
{
QString text = "row: "+QString::number(i) + "/col: "+QString::number(j);
QStandardItem *item = new QStandardItem(QString(text));
model->setItem(i,j,item);
}
}
table->resizeRowsToContents();
table->resizeColumnsToContents();
table->setFixedSize(300,400);
QVBoxLayout *vbl = new QVBoxLayout(this);
vbl->addWidget(table,1);
setLayout(vbl);
setFixedSize(800,600);
connect(table,SIGNAL(activated(QModelIndex)),this,SLOT(updateDesc(QModelIndex)));
connect(table,SIGNAL(clicked(QModelIndex)),this,SLOT(updateDesc(QModelIndex)));
connect(this,SIGNAL(updateFromArrow(QModelIndex)),this,SLOT(updateDesc(QModelIndex)));
}
bool MainWindow::eventFilter(QObject *o, QEvent *e)
{
if(o == table && e->type() == QEvent::KeyPress)
{
//key was pressed on param detail selection...
int row = table->currentIndex().row();
int col = table->currentIndex().column();
int rowCnt = model->rowCount()-1;
int colCnt = model->columnCount()-1;
QKeyEvent *keyEv = static_cast <QKeyEvent*> (e);
if(keyEv->key() == Qt::Key_Left)
{
qDebug()<<"Key LEFT";
if(col != 0)
emit updateFromArrow(model->index(row,col-1));
else
emit updateFromArrow(model->index(row,col));
}//end if key left
else if(keyEv->key() == Qt::Key_Right)
{
if(col != colCnt)
emit updateFromArrow(model->index(row,col+1));
else
emit updateFromArrow(model->index(row,col));
}//end keyright
else if(keyEv->key() == Qt::Key_Up)
{
if(row != 0)
emit updateFromArrow(model->index(row-1,col));
else
emit updateFromArrow(model->index(row,col));
}//end if key up
else if(keyEv->key() == Qt::Key_Down)
{
if(row != rowCnt)
emit updateFromArrow(model->index(row+1,col));
else
emit updateFromArrow(model->index(row,col));
}//end if key down
}
return false;
}
void MainWindow::updateDesc(QModelIndex mi)
{
qDebug()<<"tree Clicked: row:"<<mi.row()<<"col:"<<mi.column()<<"curindxCol:"<<table->currentIndex().column();
QModelIndex m = model->index(mi.row(),mi.column());
if(mi.column() == 0) //if we are in first column, then jump to 2nd column <-works on clicking, not on keypress from eventFilter
{
qDebug()<<"Changing index, in col 0 for param";
m = model->index(mi.row(),1);
//table->selectionModel()->select(table->model()->index(mi.row(),1), QItemSelectionModel::ClearAndSelect);
table->setCurrentIndex(m);
}
}
MainWindow::~MainWindow()
{
}
To copy to clipboard, switch view to plain text mode
finally file: mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtGui/QMainWindow>
#include <QtGui>
{
Q_OBJECT
public:
~MainWindow();
signals:
public slots:
protected:
};
#endif // MAINWINDOW_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtGui/QMainWindow>
#include <QtGui>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
QStandardItemModel *model;
QTableView *table;
signals:
void updateFromArrow(QModelIndex);
public slots:
void updateDesc(QModelIndex);
protected:
bool eventFilter(QObject *o, QEvent *e);
};
#endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode
Bookmarks