我想用方格做一张8*8的桌子(棋盘)。现在,我有了制作表格的代码,但不知道如何调整单元格的大小,使之成为方形。
我也想把碎片的照片放进牢房里。我该怎么做?
下面是我的代码:
#include <QtGui/QApplication>
#include "mainwindow.h"
#include <QHBoxLayout>
#include <QTableWidget>
class Table : public QWidget
{
public:
Table(QWidget *parent = 0);
};
Table::Table(QWidget *parent)
: QWidget(parent)
{
QHBoxLayout *hbox = new QHBoxLayout(this);
QTableWidget *table = new QTableWidget(8 , 8 , this);
hbox->addWidget(table);
setLayout(hbox);
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Table t;
t.show();
return a.exec();
}编辑:
如果有人能帮我加载一个图像作为背景的细胞,这将是非常感谢!我使用此代码,编译器不会生成错误,但程序无法运行。我认为问题在于table->item(0,0)。我应该先初始化它吗?
QString fileName = "1.bmp";
QPixmap pic (fileName);
QIcon icon (pic);
table->item(0,0)->setIcon(icon);发布于 2010-07-08 19:00:37
为了使细胞呈方形,可以这样做:
// set the default size, here i've set it to 20px by 20x
table->horizontalHeader()->setDefaultSectionSize(20);
table->verticalHeader()->setDefaultSectionSize(20);
// set the resize mode to fixed, so the user cannot change the height/width
table->horizontalHeader()->setResizeMode(QHeaderView::Fixed);
table->verticalHeader()->setResizeMode(QHeaderView::Fixed);编辑:设置图像,在QTableWidgetItem上设置图标属性
发布于 2010-07-09 10:10:16
在搜寻之后.我终于得到了答案。我应该首先创建一个QBrush对象并将其设置为QtableWidgetItem的背景,然后使用表->setItem!
QString fileName = "/1.bmp";
QPixmap pic (fileName);
QBrush brush(pic);
QTableWidgetItem* item = new QTableWidgetItem();
item->setBackground(brush);
table->setItem(0,0,item);https://stackoverflow.com/questions/3206925
复制相似问题