2
votes

I retrieve a file list from a folder. For each file I would like to put its date (QString) and a QLabel. For each file these three elements would be put in a QListWidget. I recover all the files and the date correctly. The problem is that these two elements are in two different items. Moreover with this method, I can not put the QLabel in the QListWidget.

Here is my code :

viewList.h :

class viewList : public QWidget{
Q_OBJECT

public:
viewList();
QString getDate();

private:
QGridLayout *gridlayout;
QHBoxLayout *hboxList;
QVBoxLayout *vboxlist;
QPushButton *button;
QLabel *myLabel;
QListWidget *listwidget;

};

viewList.cpp:

viewList::viewList(){

gridlayout=new QGridLayout;
vboxlist=new QVBoxLayout;
hboxList=new QHBoxLayout;
//Button is outside the list 
button=new QPushButton("test",this);
myLabel=new QLabel("ok",this);

QString path="/home/myFolder";
listwidget=new QListWidget;

foreach(QString file, files){
    listwidget->addItem(file);
    listwidget->addItem(getDate());
}
vboxlist->addWidget(listwidget);
gridlayout->addWidget(button,0,0,1,1);
gridlayout->addLayout(vboxlist,1,0,1,1);
this->setLayout(gridlayout);

}

QString viewList::getDate(){
  return QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
}

My three problems is that if I do listwidget-> addItem (myLabel) it does not work.In addition I would like to put the file and the label in a QHBoxLayout and the QHBoxLayout in a QVBoxLayout. But with my method I can not do it. Finally I would like QLabel, QString (date) and QString (file) to be in the same item.

Would anyone have an idea of ​​the method for doing this? For now I just show the file and the date in two different items.

2

2 Answers

3
votes

Try:

 QString widgetItem;
 widgetItem = file +" "+ getDate() +" "+ (label->text());
 listwidget->addItem(widgetItem);

UPD:

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include<QDebug>
#include<QHBoxLayout>
#include<QWidget>
#include<QLabel>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
    ui->setupUi(this);

}
MainWindow::~MainWindow()
{
    delete ui;
}



void MainWindow::on_pushButton_clicked()
{
//Creating our widget for composing
QWidget* window = new QWidget();
//Creating a new label
QLabel *label = new QLabel();
label->setText("MyLable");
label->setAlignment(Qt::AlignCenter);
 //Creating a new button for crowding
QPushButton* button = new QPushButton("hey");

//Creating a new list widget item whose parent is the listwidget itself
QListWidgetItem* item;
item = new QListWidgetItem(ui->listWidget);
item->setSizeHint(QSize(0,30)); // you could change it

//Creating layout for our label and etc...
QHBoxLayout* layout = new QHBoxLayout();
//Adding elements to layout
layout->addWidget(label);
layout->addWidget(button); // just for example
layout->setAlignment(label,Qt::AlignCenter);
layout->setAlignment(button,Qt::AlignCenter);
layout->addStretch();
window->setLayout(layout);

//Adding the item to the listwidget
ui->listWidget->addItem(item);

ui->listWidget->setItemWidget(item,window);

QString widgetText;
QString fileName = "fileName";
widgetText= fileName +" "+ "Date";

item->setText(str);
item->setTextAlignment(Qt::AlignRight|Qt::AlignCenter);

//setting style for label in listWidget
label->setStyleSheet("color: white; background: red;");

} I create a new item when you click on the button.

UPD2:

QStringList files;
files<<"file1"<<"file2"<<"file3";
foreach (QString file, files) {
     QLabel *labelDate = new QLabel();
     QLabel *labelFile = new QLabel();
     QLabel *Mylabel = new QLabel();
     Mylabel->setText("Something");
     labelDate->setText("someDate");
     labelFile->setText(file);
     labelDate->setStyleSheet("color: white; background: red;");
     labelFile->setStyleSheet("color: white; background: red;");
     Mylabel->setStyleSheet  ("color: white; background: red;");


QWidget* window = new QWidget();
QListWidgetItem *item = new QListWidgetItem(ui->listWidget);

item->setSizeHint(QSize(0,30)); // you could change it

//Creating layout for our label and etc...
QHBoxLayout* layout = new QHBoxLayout();
//Adding elements to layout
layout->addWidget(labelDate);
layout->addWidget(labelFile); // just for example
layout->addWidget(Mylabel);
layout->addStretch();
window->setLayout(layout);
ui->listWidget->addItem(item);
ui->listWidget->setItemWidget(item,window);

}
0
votes

For a list of items with several columns I found QTreeWidget to be better suited. You just don't use the hierarchical feature (all entries are topLevelItems).

This allows all advantages of a tabular model: Style sheet per field, different base QVariant types, sorting per column, embedding images etc...

Using a QTableWidget is more difficult, because each field will be a distinct item.