: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/usieve/stoopidsimple.com/includes/common.inc on line 1782.
: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/usieve/stoopidsimple.com/includes/common.inc on line 1782.
Qt 4 QTableWidget Tips and Tricks | stoopidsimple.com

Qt 4 QTableWidget Tips and Tricks

salem | 2005-09-24 02:57

How to hide headers
How to set header labels
How to alternate colors for rows
How to set defaults for table items


How to hide headers
To hide the vertical or horizontal headers use:

#include <QTableWidget>
// Don't forget the QHeaderView include!
// You can compile QTableWIdget code without it,
// but you will not have the prototypes for
// manipulating QHeaderView's!
#include <QHeaderView>
...
table->verticalHeader()->hide();
table->horizontalHeader()->hide();

How to set header labels
The easiest method for setting header labels is to use a QStringList and QTableView::set(Vertical|Horizontal)HeaderLabels( const QStringList& ). Here is an example:

QStringList sl;
// The following seems like a non-intuitive operator
// overload but that is how they do it...
sl << "First Column" << "Next Column" << "Last Column";
table->setHorizontalHeaderLabels(sl);

Alternating Colors
No need to implement your own alternating color scheme to make your table look modern, just use:

table->setAlternatingRowColors();

A better way to set defaults for items
It looks like the Trolltech guys intend on implementing a feature where QTableWidgets hold a "prototype" QTableWidgetItem used to clone new items through a "createItem()" function. However, they have not yet implemented this feature. Here is a way to mimic the idea using the current codebase:

QTableWidgetItem *prototype = new QTableWidgetItem();
// setup your prototype
prototype->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled);
// add all your items using the prototype->clone() method
while( more items ) {
  QTableWidgetItem *item = prototype.clone();
  // set the special features of each item
  item->setText("item foo");
}

Lfhex

Tonality

User login

Navigation

PHP

Kccmp

Java

Homestar

Slashdot


Advertisement