1 Attachment(s)
how i can chagne Qtableview horizontal header text as well as remove Indexes vHeader
Hi every one
i'm using qsqlquerymodel to get data from sqlite database and Qtableview widget to retrive data
i want to custimize ( vertical header and horizontal header in Qtableview )
vertical header ---> remove indexes numbers but vertical header still shown and place icon when the entier row is selected
horizontal header ---> change the text of caption
her what i code
Code:
// for horizontal header
ProduitModel->setHeaderData(0,Qt::Horizontal, "Id");
ProduitModel->setHeaderData(1,Qt::Horizontal, "Designation");
ProduitModel->setHeaderData(2,Qt::Horizontal, "Famille");
ProduitModel->setHeaderData(3,Qt::Horizontal, "Qte Maximum");
ProduitModel->setHeaderData(4,Qt::Horizontal, "Qte Minimum");
ProduitModel->setHeaderData(5,Qt::Horizontal, "Prix");
ProduitModel->setHeaderData(6,Qt::Horizontal, "Prix vente");
// form vertical header
ProduitView->verticalHeader()->setStyleSheet("QHeaderView::section:checked {"
" background-color: black; }"
" background-image: url(imgs/arrow.png)"
);
Her's image that explain what i mean
Attachment 9291
Re: how i can chagne Qtableview horizontal header text as well as remove Indexes vHea
Write a proxy model derived from QIdentityProxyModel, and implement the QIdentityProxyModel::headerData() function and return what ever data is required in in header.
In Qt::Vertical header, return nothing for Qt::ItemDataRole and return icon for Qt::DecorationRole
In Qt::Horizontal header return the desired label in Qt::ItemDataRole
Re: how i can chagne Qtableview horizontal header text as well as remove Indexes vHea
thank's Santosh but really, i havn't any idea how to do it
can you give an example OR some hints to start coding ??
Re: how i can chagne Qtableview horizontal header text as well as remove Indexes vHea
Here is fully functional code with usage example
Code:
class HeaderModel : public QIdentityProxyModel
{
public:
explicit HeaderModel
(QObject * parent
= 0) : QIdentityProxyModel(parent)
{ }
QVariant headerData
(int section, Qt
::Orientation orientation,
int role
) const {
if(orientation == Qt::Horizontal)
{
if(role == Qt::DisplayRole)
switch(section)
{
case 0: return "Id"; break;
case 1: return "Designation"; break;
case 2: return "Famille"; break;
case 3: return "Qte Maximum"; break;
case 4: return "Qte Minimum"; break;
case 5: return "Prix"; break;
case 6: return "Prix vente"; break;
default:
return QString("Column %1").
arg(section
+ 1);
break;
}
}
else if(orientation == Qt::Vertical)
{
if(role == Qt::DecorationRole)
return QIcon("imgs/arrow.png");
}
}
};
//usage
{
...
HeaderModel headerModel;
headerModel.setSourceModel(&sqlQueryModel);
tableView.setModel(&headerModel);
tableView.show();
}
Re: how i can chagne Qtableview horizontal header text as well as remove Indexes vHea
Quote:
Originally Posted by
Santosh Reddy
Here is fully functional code with usage example
Code:
class HeaderModel : public QIdentityProxyModel
{
public:
explicit HeaderModel
(QObject * parent
= 0) : QIdentityProxyModel(parent)
{ }
QVariant headerData
(int section, Qt
::Orientation orientation,
int role
) const {
if(orientation == Qt::Horizontal)
{
if(role == Qt::DisplayRole)
switch(section)
{
case 0: return "Id"; break;
case 1: return "Designation"; break;
case 2: return "Famille"; break;
case 3: return "Qte Maximum"; break;
case 4: return "Qte Minimum"; break;
case 5: return "Prix"; break;
case 6: return "Prix vente"; break;
default:
return QString("Column %1").
arg(section
+ 1);
break;
}
}
else if(orientation == Qt::Vertical)
{
if(role == Qt::DecorationRole)
return QIcon("imgs/arrow.png");
}
}
};
//usage
{
...
HeaderModel headerModel;
headerModel.setSourceModel(&sqlQueryModel);
tableView.setModel(&headerModel);
tableView.show();
}
Wow Reddy,
Your code Work perfectly, but i have Qt 4.7 witch mean that QIdentityProxyModel is not supported. So, i have used QSortFilterProxyModel
the horziontal header changed with my text , and vertical header to arrow instead of numbers
i need the vertical header show arrow.png only when the user select the row , what's the Role that allow me to do this ?
Re: how i can chagne Qtableview horizontal header text as well as remove Indexes vHea
Oops... Sorry I din't see that you using Qt4, anyway you have figured out a way.
For icon use Qt::DecorationRole
Re: how i can chagne Qtableview horizontal header text as well as remove Indexes vHea
Quote:
Originally Posted by
Santosh Reddy
Oops... Sorry I din't see that you using Qt4, anyway you have figured out a way.
For icon use Qt::DecorationRole
Sorry, i don't noticed that i'm using Qt 4.7
any way Qt::DecorationRole get all vertical index filled by arrow.png icon , i want the icon only shown when the user select the entier row
Re: how i can chagne Qtableview horizontal header text as well as remove Indexes vHea
Looks like it is not supported to change the icon/image on the header only when the row/column is selected. Only thing that works is backgroud color using stylesheets
Code:
tableView.setStyleSheet("QHeaderView::section:checked { background-color: red; }");
1 Attachment(s)
Re: how i can chagne Qtableview horizontal header text as well as remove Indexes vHea
Maybe , but i have an application use this action
Attachment 9299
Re: how i can chagne Qtableview horizontal header text as well as remove Indexes vHea
Ok I can think of a way to do this. We can create and install a custom QHeaderView on QTableView's verticalheader. Here a revised solution.
Code:
// Handles Horizontal header
{
Q_OBJECT
public:
explicit HeaderModel
(QObject * parent
= 0) { }
QVariant headerData
(int section, Qt
::Orientation orientation,
int role
) const {
if(orientation == Qt::Horizontal)
{
if(role == Qt::DisplayRole)
switch(section)
{
case 0: return "Id"; break;
case 1: return "Designation"; break;
case 2: return "Famille"; break;
case 3: return "Qte Maximum"; break;
case 4: return "Qte Minimum"; break;
case 5: return "Prix"; break;
case 6: return "Prix vente"; break;
default:
return QString("Column %1").
arg(section
+ 1);
break;
}
}
}
};
// Handles Vertical header
{
public:
, mSelectedIndex(-1)
{
setMinimumWidth
(QImage("imgs/arrow.png").
width());
}
protected:
{
int section = logicalIndexAt(event->pos());
if(mSelectedIndex != section)
{
emit sectionClicked(section);
emit sectionPressed(section);
emit sectionEntered(section);
mSelectedIndex = section;
update();
}
}
void paintSection
(QPainter *painter,
const QRect &rect,
int logicalIndex
) const {
painter->save();
if(mSelectedIndex > -1)
{
if(mSelectedIndex == logicalIndex)
if(!isSectionHidden(logicalIndex))
painter
->drawImage
(rect,
QImage("imgs/arrow.png"));
}
painter->restore();
}
private:
int mSelectedIndex;
};
//Usage
HeaderModel headerModel;
headerModel.setSourceModel(&model); //<<<< Horizontal Header
tableView.setVerticalHeader(new Header(&tableView)); //<<<< Vertial Header
tableView.setModel(&headerModel);
tableView.show();
Re: how i can chagne Qtableview horizontal header text as well as remove Indexes vHea
thank's it's nearly what i need ,
how i can handle when the user use up and down keyboard keys
with keyPressEvent ???
Re: how i can chagne Qtableview horizontal header text as well as remove Indexes vHea
how i can handle this , only one the user use Key_Up and Key_Down
Re: how i can chagne Qtableview horizontal header text as well as remove Indexes vHea
what should i learn to be able to custimise Qtableview completly
Re: how i can chagne Qtableview horizontal header text as well as remove Indexes vHea
Re: how i can chagne Qtableview horizontal header text as well as remove Indexes vHea
Quote:
Originally Posted by
rockdemon
thanks, this is a great book
Added after 12 minutes:
any body can help me ??
Re: how i can chagne Qtableview horizontal header text as well as remove Indexes vHea
something like? http://qt.developpez.com/doc/4.7/modelview/
They're not all great - but there are some examples on t'internet if you google :)
The book above does have the best walkthrough i've found for model view in Qt.
Re: how i can chagne Qtableview horizontal header text as well as remove Indexes vHea
Quote:
Originally Posted by
rockdemon
something like?
http://qt.developpez.com/doc/4.7/modelview/
They're not all great - but there are some examples on t'internet if you google :)
The book above does have the best walkthrough i've found for model view in Qt.
thanks i will read page by page
Added after 13 minutes:
Her is detailled description for my problem
i use Qsqlquerymodel with sqlite3 DBMS
to retrieve data from database i use two Qtableview widgets
i want to the user to be able to work like so:
the user uses only keyboard(no mouse )
when the data grabed from database and showed in the Qtableview via Qsqlquerymodel the first row in the table must be selected by default
the user uses arrow keys ( key_Down and Key_Up) to move between rows with "arrow.png" icon shown in verticalheader of Qtableview
when the user clics Entrer_Key, the row selected will be appened in the second Qtableview
plz help me , i have read model/view many times , and i figured out what really mean , but i still have a porbleme with customizing mytable view to work with
Qkeymouse events, currentrowchanged signal
Re: how i can chagne Qtableview horizontal header text as well as remove Indexes vHea
Re: how i can chagne Qtableview horizontal header text as well as remove Indexes vHea
plz help me, i need the soulution
Re: how i can chagne Qtableview horizontal header text as well as remove Indexes vHea
Here you go, keyboard only operation
Code:
// Handles Horizontal header
{
Q_OBJECT
public:
explicit HeaderModel
(QObject * parent
= 0) { }
QVariant headerData
(int section, Qt
::Orientation orientation,
int role
) const {
if(orientation == Qt::Horizontal)
{
if(role == Qt::DisplayRole)
switch(section)
{
case 0: return "Id"; break;
case 1: return "Designation"; break;
case 2: return "Famille"; break;
case 3: return "Qte Maximum"; break;
case 4: return "Qte Minimum"; break;
case 5: return "Prix"; break;
case 6: return "Prix vente"; break;
default:
return QString("Column %1").
arg(section
+ 1);
break;
}
}
}
};
// Handles Vertical header
{
Q_OBJECT
public:
, mSelectedIndex(-1)
{
setMinimumWidth
(QImage("imgs/arrow.png").
width());
}
public slots:
{
int section = current.row();
if(mSelectedIndex != section)
{
mSelectedIndex = section;
viewport()->update();
}
}
protected:
void paintSection
(QPainter *painter,
const QRect &rect,
int logicalIndex
) const {
painter->save();
if(mSelectedIndex > -1)
{
if(mSelectedIndex == logicalIndex)
if(!isSectionHidden(logicalIndex))
painter
->drawImage
(rect,
QImage("imgs/arrow.png"));
}
painter->restore();
}
private:
int mSelectedIndex;
};
//Usage
HeaderModel headerModel;
headerModel.setSourceModel(&model);
Header * header = new Header(&tableView);
tableView.setModel(&headerModel);
tableView.setVerticalHeader(header);
tableView.show();