#include <QApplication>
#include <QWidget>
#include <QTableWidget>
#include <QHeaderView>
#include <QString>
{
};
int main(int argc, char *argv[])
{
//instance of table
TableWidget t;
// specifies size of tablewidget (window size)
int w=640,h=480;
// specifies column and row count
int row=3,col=4;
//specifies each cell size of table
int cellr,cellc;
t.resize(w,h); //setting window size to width and height specified by user
//sets background color
palet.setColor( t.backgroundRole(), Qt::black );
t.setPalette(palet);
//sets selection color i.e, changes color when item is focused.
t.setStyleSheet("selection-background-color:cyan");
//hides headers
t.verticalHeader()->hide();
t.horizontalHeader()->hide();
//calculating cell size based on number of row and column using width and height
cellr=h/row;
cellc=w/col;
t.verticalHeader()->setDefaultSectionSize(cellr);
t.horizontalHeader()->setDefaultSectionSize(cellc);
// set the resize mode to fixed, so the user cannot change the height/width
t.
horizontalHeader()->setResizeMode
(QHeaderView::Fixed);
t.setRowCount(row);
t.setColumnCount(col);
//sets focus policy;
t.setFocusPolicy(Qt::StrongFocus);
for (int r = 0; r < t.rowCount(); ++r)
{
for (int c = 0; c < t.columnCount(); ++c)
{
item->setBackgroundColor(Qt::black);
item
->setIcon
(QIcon(QString(":/images/img%1.png").
arg(r
* t.
columnCount() + c
)));
t.setItem(r, c, item);
}
}
t.show();
return app.exec();
}
#include <QApplication>
#include <QWidget>
#include <QTableWidget>
#include <QHeaderView>
#include <QString>
class TableWidget : public QTableWidget
{
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
//instance of table
TableWidget t;
// specifies size of tablewidget (window size)
int w=640,h=480;
// specifies column and row count
int row=3,col=4;
//specifies each cell size of table
int cellr,cellc;
t.resize(w,h); //setting window size to width and height specified by user
//sets background color
QPalette palet;
palet.setColor( t.backgroundRole(), Qt::black );
t.setPalette(palet);
//sets selection color i.e, changes color when item is focused.
t.setStyleSheet("selection-background-color:cyan");
//hides headers
t.verticalHeader()->hide();
t.horizontalHeader()->hide();
//calculating cell size based on number of row and column using width and height
cellr=h/row;
cellc=w/col;
t.verticalHeader()->setDefaultSectionSize(cellr);
t.horizontalHeader()->setDefaultSectionSize(cellc);
// set the resize mode to fixed, so the user cannot change the height/width
t.horizontalHeader()->setResizeMode(QHeaderView::Fixed);
t.verticalHeader()->setResizeMode(QHeaderView::Fixed);
t.setRowCount(row);
t.setColumnCount(col);
//sets focus policy;
t.setFocusPolicy(Qt::StrongFocus);
for (int r = 0; r < t.rowCount(); ++r)
{
for (int c = 0; c < t.columnCount(); ++c)
{
QTableWidgetItem* item = new QTableWidgetItem;
item->setBackgroundColor(Qt::black);
item->setIcon(QIcon(QString(":/images/img%1.png").arg(r * t.columnCount() + c)));
t.setItem(r, c, item);
}
}
t.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
---------------------------------------------------------------------------------------------------------------
Bookmarks