![]() |
| ||
Classes - Annotated - Tree - Functions - Home - Structure |
The following example shows how to populate the cells of a spreadsheet with pixmaps and text, combo boxes and check boxes.
Our aim is a table that consists of 100 columns and 100 rows, with the entire second column filled with check boxes. The sixth column is filled with combo boxes where users might choose between four items. The third cell in the fourth row contains a pixmap and a string.
#include <qapplication.h> #include <qtable.h> #include <qimage.h> #include <qpixmap.h> #include <qstringlist.h>
All we need to implement this are five classes: QApplication (overall it's a GUI program), QTable for the spreadsheet, QImage to load and scale the icon, QPixmap to show it, and QStringList to store the combo box items.
#include "qtlogo.xpm"
qtlogo.xpm contains the qtlogo_xpm pixmap we're going to use.
Next we define the table size: 100 columns and 100 rows.
In the main program the first thing for us to do is to create "the application" itself.
Then we create a table widget of the defined size.
Next we load the pixmap we want to show. We use a QImage for this purpose because we don't know whether the icon fits into the table cell.
Scaling and other transformation operations are efficiently applied to QImage objects whilst to transform a QPixmap Qt has to obtain it from the windowing system where QPixmaps are stored, do the transformation, and hand the transformed pixmap back to the windowing system.
We scale the image to fit into the cell. As we want to place it in the fourth row, the limiting factor is the height of this row (keep in mind that row and column numbering in QTable starts with zero so that the fourth row is denoted by 3).
We assign the resulting QImage to a QPixmap. This hands the pixmap over to the windowing system, so that we can use it for displaying purposes.
The scaled pixmap now becomes the content of the cell where fourth row and third column meet.
Then we add the text string A Pixmap to the right of the pixmap.
We mentioned earlier that we intend to populate the entire sixth column with combo boxes. A combo box with no entries to choose from is quite boring. Thus we define a string list comboEntries to store them.
The combo boxes will hold four entries, boringly named one to four.
Beginning with the first row, row after row, until we are done with the last row (i.e. no. numRows-1) ...
... we wish to populate the sixth column with identical combo boxes showing the entries one, two, three and four. Fortunately there is a special class to fill table cells with combo boxes named QComboTableItem.
Thus we create a QComboTableItem object for every column. As we set the last parameter to TRUE the user has the possibility to change the text of the combo box entries.
Although all of the combo table items are the same we want them to show different initial values: Instead of having an entire column boringly showing a one as current value of each cell, we prefer one, two, three, four, one, two etc. top-down.
To achieve this we define that the first and every fourth combo box initially show the first menu entry (the row number modulo 4 equals 0 and therefore uses the one entry), the second and every fourth combo table item show two, and so forth.
Finally we fill the 5th column with the prepared combo box items.
Similarly the entire second column is filled with unchecked check boxes of the title Check me.
So we are done with creating the table. We make it the main window of the Qt application,
show it,
and start the event loop of the GUI.
Copyright © 2001 Trolltech | Trademarks | Qt version 3.0.0-beta2
|