PDA

View Full Version : Qt: Space between individual cells in QTableWidget



91JustCurious
9th February 2016, 09:25
(Using Qt 5.5.1 on Windows 8.1)

I have a table displaying images selected by the user.

I know their are many other ways to display multiple images on a GUI, but I'm new to Qt, so I didn't understand how to use QGraphicsView and found the following to be the easiest way.

But this forms a table in which images are not separated. I want some space b/w them. See how the next images starts right where the first ends.

11684

How can I do it so that next image starts after leaving some space?



QFileDialog dialog(this);
dialog.setNameFilter(tr("Images (*.jpg)"));
dialog.setFileMode(QFileDialog::ExistingFiles);
QStringList all_filenames = dialog.selectedFiles();
int maxCol = 3;
int maxRows = all_filenames.size() / maxCol;
ui->tableWidget->setColumnCount(maxCol);
ui->tableWidget->setRowCount(maxRows);
int remainder = all_filenames.size() % maxCol;
if (remainder != 0)
{
maxRows +=1;
}
ui->tableWidget->horizontalHeader()->setDefaultSectionSize(200);
ui->tableWidget->verticalHeader()->setDefaultSectionSize(200);
if(all_filenames.isEmpty() == 0)
{
for( int i = 0; i < all_filenames.size() ; ++i)
{
QPixmap map(all_filenames.at(i));
map = map.scaled(200,200,Qt::IgnoreAspectRatio,Q::FastTr ansformation);
QBrush brush(map);
QTableWidgetItem* item = new QTableWidgetItem();
item->setCheckState(Qt::CheckState());
item->setBackground(brush);
ui->tableWidget->setItem(j,k,item);
k++;
if ( k == maxCol )
{
j++;
k = 0;
}


}
}

anda_skoa
9th February 2016, 09:50
You set the image size to exactly the same size as the cell size.
Have you tried making the images slightly smaller than the cell size?
E.g. 180x180?

Cheers,
_