#include <QtGui>
#include <QDebug>
Q_OBJECT
public:
m_blankPixmap("blank.png"),
m_otherPixmap("other.png")
{
for(int r = 0; r < 10; ++r) {
for(int c = 0; c < 10; ++c) {
label->setPixmap(m_blankPixmap);
m_layout->addWidget(label, r, c, Qt::AlignCenter);
}
}
central->setLayout(m_layout);
setCentralWidget(central);
setCellPixmap(3, 3, m_otherPixmap);
}
void setCellPixmap
(int row,
int col,
const QPixmap &pixmap
) { QLabel *label
= qobject_cast<QLabel
*>
( m_layout
->itemAtPosition
(row, col
)->widget
() );
if (label) {
label->setPixmap(pixmap);
}
}
private:
};
int main(int argc, char *argv[])
{
MainWindow m;
m.show();
return app.exec();
}
#include "main.moc"
#include <QtGui>
#include <QDebug>
class MainWindow: public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *p = 0):
QMainWindow(p) ,
m_blankPixmap("blank.png"),
m_otherPixmap("other.png")
{
QWidget *central = new QWidget(this);
m_layout = new QGridLayout(central);
for(int r = 0; r < 10; ++r) {
for(int c = 0; c < 10; ++c) {
QLabel *label = new QLabel(central);
label->setPixmap(m_blankPixmap);
m_layout->addWidget(label, r, c, Qt::AlignCenter);
}
}
central->setLayout(m_layout);
setCentralWidget(central);
setCellPixmap(3, 3, m_otherPixmap);
}
void setCellPixmap(int row, int col, const QPixmap &pixmap) {
QLabel *label = qobject_cast<QLabel*>( m_layout->itemAtPosition(row, col)->widget() );
if (label) {
label->setPixmap(pixmap);
}
}
private:
QGridLayout *m_layout;
const QPixmap m_blankPixmap;
const QPixmap m_otherPixmap;
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow m;
m.show();
return app.exec();
}
#include "main.moc"
To copy to clipboard, switch view to plain text mode
Bookmarks