I want to add items dynamically to QListWidget in Qt.
I have used the following piece of code to add items dynamically, but I am able to add only one item in QListWidget...
for(int i=0; i<5; i++)
{
structLocationDetails[i].strlocationName = metaresult["locationName"];
QString strtemp = structLocationDetails[i].strlocationName;
list=new QListWidget(this);
list->setSortingEnabled(true);
list->setGeometry(0,0,190, 450);
QStringList items;
item1=new QListWidgetItem(QIcon(":/imagesIcon.png"),structLocationDetails[i].strlocationName,list);
connect(list,SIGNAL(itemClicked(QListWidgetItem*)),this,SLOT(myitem(QListWidgetItem*)));
}
Here actually I am parsing an XML file and trying to add the parsed xml file contents(locationName) to the QListWidget.
How can I achieve this?
Please provide me any suggestions on this...
Thanks...
changing the code do the job...
list=new QListWidget(this);
list->setSortingEnabled(true);
list->setGeometry(0,0,190, 450);
connect(list,SIGNAL(itemClicked(QListWidgetItem*)),this,SLOT(myitem(QListWidgetItem*)));
for(int i=0; i<5; i++)
{
structLocationDetails[i].strlocationName = metaresult["locationName"];
item1=new QListWidgetItem(QIcon(":/imagesIcon.png"),structLocationDetails[i].strlocationName,list);
}
Related
How I can to insert the value from txt file in to the Qlist...
QList<QString> list_StRead;
list_StRead.insert();
I can sorting txt file ... its mean that my file is a line by line. than after the insert to the Qlist I want to write in to Qtabelewidget. How I must to do?? u must to be completely understand. see the img file
tnx for all....
Here is the pseudo code.
Please try it. (Not tested, code written in notepad. excuse me any syntax errors).
//Your list
QList<QString> list_StRead;
//Text stream object read data and insert in list.
QTextStream in(&file);
while (!in.atEnd())
{
QString line = in.readLine(); //read one line at a time
list_StRead.push_back(line);
}
//loop your list
for(int i =0; i<list_StRead.size(); i++)
{
//Add by create your tablewidget item and append it.
YourTableWidget->setItem(rowNumber,colNumber,new QTableWidgetItem(list_StRead.at(i)))
}
I have a QMenu, where I want to add QActions. The problem is, everytime the table is clicked, the existing menu adds onto itself. How do I stop it from doing that?
So when I click on the table, it should be new menus everytime.
void MainWindow::onTableClicked(const QModelIndex &index){
QAction *action;
// action->setAutoRepeat(false);
if (index.isValid()) {
QString cellText = index.data().toString();
QString indexRow = QString::number(index.row());
QString indexCol = QString::number(index.column());
ui->textBrowser->setText(indexRow + "\n"+ indexCol);
QSet<int> possiblevalues = findPossibleValues(index.row(),index.column());
for(int n : possiblevalues){
listofPossibleValues.push_back(QString::number(n));
}
for(auto s :listofPossibleValues){
action = myMenu.addAction("Set Value "+s);
}
}
}
The reason why it looked like myMenu.clear wasnt working was because i did not remove listofPossibleValues after I added the menuaction. I was able to fix it by adding
//At the beginning
myMenu.clear();
//At the end
listofPossibleValues.clear();
I have two columns in treewidget viz. Files and path. Now I want to dump the items into an xml file. But the issue I'm facing is that I'm unable to extract the items.
QDomDocument document;
QDomElement root = document.createElement("Playlist");
document.appendChild(root);
QMessageBox::StandardButton reply;
reply = QMessageBox::question(this, "Save", "Do you want to save the playlist?", MessageBox::Yes | QMessageBox::No);
if(reply == QMessageBox::Yes)
{
//Add some elements
QDomElement playlist = document.createElement("MyPlaylist");
playlist.setAttribute("Name", "Playlist1");
root.appendChild(playlist);
for(int h = 0; h < ui->treeWidget->topLevelItemCount(); h++)
{
QDomElement file = document.createElement("File");
file.setAttribute("ID", ui->treeWidget->columnAt(0)); //1
file.setAttribute("Path", ui->treeWidget->columnAt(1)); //2
playlist.appendChild(file);
}
}
Can anyone help me out how to handle such situation wherein multiple column items are to be read. Comments 1 & 2 are the essence of whole story.
I think, you can write it like:
[..]
for(int h = 0; h < ui->treeWidget->topLevelItemCount(); h++)
{
QDomElement file = document.createElement("File");
QTreeWidgetItem *item = ui->treeWidget->topLevelItem(h);
file.setAttribute("ID", item->text(0)); //1
file.setAttribute("Path", item->text(1)); //2
playlist.appendChild(file);
}
I'm trying to fill a QTableWidget from a file that was exported by my program, but when I try to set text to the table cells they just ignore me, nothing happens.
void MainWindow::on_actionOpen_Project_triggered()
{
QString line, fileName;
fileName = QFileDialog::getOpenFileName(this,tr("Open Project"), "", tr("Project Files (*.project)"));
if(!fileName.isEmpty()){
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) return;
QTextStream in(&file);
for(int i=0;i<ui->code->rowCount();i++){ // goes through table rows
for(int j=0;j<ui->code->columnCount();j++){ // through table columns
ui->code->setCurrentCell(i,j);
QTableWidgetItem *cell = ui->code->currentItem();
in >> line;
if(!line.isEmpty()){
cell = new QTableWidgetItem(line); // relates the pointer to a new object, because the table is empty.
ui->errorlog->append(line); // just for checking if the string is correct visually.
}
}
}
file.close();
}
}
The errorlog object shows on the screen the correct values opened from the file, however the table is not filled. Any problems found?
This line:
cell = new QTableWidgetItem(line);
doesn't do what you think it does. Assigning the cell doesn't change anything in the table – cell is just a local variable, overwriting it doesn't have any effect elsewhere.
You (probably) want to do something like this instead:
cell->setText(line);
Or (if you don't really need to change the current item):
ui->code->item(i,j)->setText(line);
If those give you a segfault, it's that you haven't set the table's widget items yet. In that case, you should do something like:
QTableWidgetItem *cell = ui->code->item(i,j);
if (!cell) {
cell = new QTableWidgetItem;
ui->code->setItem(i,j,cell);
}
if (!line.isEmpty()) {
cell->setText(line);
}
This will populate all the cells with QTableWidgetItems the first time this function is called, and reuse them subsequently.
How can I get the texts of all the widgets in a QListWidget as a QList<QString>?
I can get the list of widget items like this:
QList<QListWidgetItem *> items =
ui->listWidget->findItems(QString("*"), Qt::MatchWrap | Qt::MatchWildcard);
But that's not exactly what I want, I'd like the list of the widget text() properties.
There is no built-in function for that, you'll need to do it manually.
QList<QString> texts;
foreach(QListWidgetItem *item, items)
texts.append(item->text());
Or something like that.
int c = ui->listWidget->count();
for (int i = 0; i < c ; ++i){
QString s = QString::number(i);
QModelIndex *model_index = new QModelIndex(ui->listWidget->model()->index(i,0) ); //0th column since we have one cloumn in listwidget
QString q= model_index->data(Qt::DisplayRole).toString();
qDebug()<<q;
}