Image 调整单元格大小';s高度和宽度,并在QTableWidget中加载图像

Image 调整单元格大小';s高度和宽度,并在QTableWidget中加载图像,image,qt,resize,cell,qtablewidget,Image,Qt,Resize,Cell,Qtablewidget,我想做一张8*8的方格桌子(一个棋盘)。现在我有了制作表格的代码,但不知道如何将单元格调整为方形 我还想把碎片的照片放进细胞里。我该怎么做 以下是我的代码: #include <QtGui/QApplication> #include "mainwindow.h" #include <QHBoxLayout> #include <QTableWidget> class Table : public QWidget { public: Table(

我想做一张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();
}

要使单元格呈方形,请执行以下操作:

  // 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);

编辑:要设置图像,请在搜索、搜索、搜索后设置s上的图标属性。

。。。。我终于得到了答案。我应该首先创建一个QBrush对象并将其设置为QtableWidgetItem的背景,然后使用table->setItem

QString fileName = "/1.bmp";
QPixmap pic (fileName);

QBrush brush(pic);

QTableWidgetItem* item = new QTableWidgetItem();
item->setBackground(brush);

table->setItem(0,0,item);

绘制棋盘的有趣方法:)不过我建议使用QGraphicsView。事实上,GraphicsView的开销会更大,但会更加灵活。它将允许动画和其他有趣的事情。
QString fileName = "/1.bmp";
QPixmap pic (fileName);

QBrush brush(pic);

QTableWidgetItem* item = new QTableWidgetItem();
item->setBackground(brush);

table->setItem(0,0,item);