Adding text to existing item of QListWidget - c++

So I wanted to add an additional text to my QListWidget list with code like this:
for (int i = 0; i < ui->history->count(); i++ )
{
ui->history->item(i)->text().append(QTime::currentTime().toString());
}
This does not worked.
I've qDebugged all list items with this code:
qDebug() << "item(" << i << ")->text() : " << ui->history->item(i)->text();
After that i received this output:
item( 0 )->text() : "http://www.google.ru/?gfe_rd=cr&ei=cT6wV9PDKI-8zAXjlaCIDw"
item( 1 )->text() : "https://news.google.ru/nwshp?hl=ru&tab=wn"
item( 2 )->text() : "https://news.google.ru/news?pz=1&hl=ru&tab=nn"
item( 3 )->text() : "https://news.google.ru/news?pz=1&hl=ru&tab=nn"
Obviously this function outputs all text of item, so why cannot I append any other string there?

Implicit sharing ensures the text is not directly changed. You have to explicitly set the text value:
QString txt = ui->history->item(i)->text().append(QTime::currentTime().toString());
ui->history->item(i)->setText (txt);

text() returns the text by value, not by reference. You need to use setText to modify the text.

Related

QT5 C++, Is there a way I can get the current text of a widget within a qlist container

I need to retrieve the text in the order in which they appear on the user form. I am trying as below:
QString line = "QLineEdit";
QString combo = "QComboBox";
QList<QWidget *> childWidgets = ui->frame_3->findChildren<QWidget *>();
QStringList data;
for(auto widget : childWidgets){
if(widget->metaObject()->className() == line || widget->metaObject()->className() == combo){
data.append(widget->text()); //append the text of the lineEdits and ComboBoxes to data
}
}
I get the following compile error from the above code:
"no member named 'text' in QWidget
Since you pointed out the QWidget base class does not have a text member function you will need to access the QComboBox and QLineEdit directly to get the current text.
QList<QWidget *> childWidgets = ui->frame_3->findChildren<QWidget *>();
QStringList data;
for(auto widget : childWidgets){
auto combo = dynamic_cast<QComboBox*>(widget);
if (combo) {
data << combo->currentText(); // currentText() returns the text from the combobox
}
else {
auto lineEdit = dynamic_cast<QLineEdit*>(widget);
if (lineEdit) {
data << lineEdit->text(); // A line edit has a text() member.
}
}
}
This code does not handle ordering. I believe the order is in the same order as added to the parent.

Id of an item in QTreeWidget changed

There is something very strange about Qt.
I have a button ui->addPointButton and a QtreeWidget ui->pointListBox. When I click on the button, it adds a point to the tree. mScenePtr is a pointer to the class I put all my points.AddPoint is a class creating a window asking for some information about the point.
void AddPointsWindow::on_addPointButton_clicked(bool clicked)
{
Q_UNUSED(clicked);
AddPoint addPointWindow(mScenePtr->getColor_or_texture());
int addPointWindowResult = addPointWindow.exec();
if (addPointWindowResult == QDialog::Accepted)
{
SVertex vertex = addPointWindow.getVertex();
mScenePtr->addVertex(vertex);
QTreeWidgetItem* itemPtr = new QTreeWidgetItem(ui->pointListBox);
cout << "id" << ui->pointListBox->indexOfTopLevelItem(itemPtr) << endl;
//itemPtr->setText(0,QString::number(mScenePtr->getVertexNumber()));
//itemPtr->setText(0, QString::number(ui->pointListBox->indexOfTopLevelItem(itemPtr)));
itemPtr->setText(0, "hjhjh");
cout << "id" << ui->pointListBox->indexOfTopLevelItem(itemPtr) << endl;
itemPtr->setText(1, QString::number(vertex.x));
itemPtr->setText(2, QString::number(vertex.y));
itemPtr->setText(3, QString::number(vertex.z));
if (color == mScenePtr->getColor_or_texture())
{
itemPtr->setText(4, QString::number(vertex.r));
itemPtr->setText(5, QString::number(vertex.g));
itemPtr->setText(6, QString::number(vertex.b));
}
//ui->pointListBox->insertTopLevelItem(ui->pointListBox->topLevelItemCount(), itemPtr);
cout << "value : " << vertex.x << endl;
}
}
In this exemple, I click twice on the buttons, create two points with vertex.x = 0 for the first and 1 for the second.
Look at the three lines in the middle :
//itemPtr->setText(0,QString::number(mScenePtr->getVertexNumber()));
//itemPtr->setText(0, QString::number(ui->pointListBox->indexOfTopLevelItem(itemPtr)));
itemPtr->setText(0, "hjhjh");
If there is only the third line, the result is
id0
id0
value : 0
id1
id1
value : 1
Everythong is ok.
But if I put one of the two others lines, the result in both cases is :
id0
id0
value : 0
id1
id0
value : 1
How is it possible ? How can the call to ui->pointListBox->indexOfTopLevelItem(itemPtr) or mScenePtr->getVertexNumber() can change the id of the item ?
Qt 5.5
After using setText, the items in the tree might have sorted automatically.
So in the two commented line cases, when you add the number (using setText), the nodes are getting sorted and the earlier node has become the top level item.
That is the reason you are seeing two different IDs "before setText" and "after setText", when you are querying "top level item".
To see the results properly, Turn off the sorting for the tree. (may be in your constructor)
ui->pointListBox->setSortingEnabled(false);

Getting variable from widget in a QListWidget

I have a custom QWidget class called VideoWidget. Its source file looks something like this:
VideoWidget::VideoWidget(QWidget *parent, string test) :
QWidget(parent)
{
pathname=test;
QLabel *label= new QLabel(pathname.c_str(), this);
//...
}
string VideoWidget::getFilePath(){
return pathname;
}
In my MainWindow class I add the VideoWidget to a QListWidget through looping through a xml file and getting the string argument from that file like this:
QDomNode node = rootXML.firstChild();
while( !node.isNull() )
{
if( node.isElement() )
{
QDomElement element = node.toElement();
VideoWidget* mytest = new VideoWidget(this, element.attribute( "Pathname", "not set").toStdString());
QListWidgetItem* item = new QListWidgetItem;
item->setSizeHint(QSize(150,100));
ui->myList->addItem(item);
ui->myList->setItemWidget(item,mytest);
}
node = node.nextSibling();
}
This correctly fills my QListWidget with the VideoWidget where all the labels have a different value.
Now I'd like to get the pathname variable everytime I doubleclick on a item in the QListWidget like this:
connect(ui->myList,SIGNAL(doubleClicked(QModelIndex)),this,SLOT(playClip(QModelIndex)));
void MainWindow::playClip(QModelIndex index){
QListWidgetItem* item = ui->myList->itemAt(0,index.row());
VideoWidget* widget = dynamic_cast<VideoWidget*>(ui->myList->itemWidget(item));
cout << widget->getFilePath() << endl;
}
My problem is that widget->getFilePath() always returns the same value for every clicked widget. It is the value of the first time I set pathname=test;. What am I missing here?
This is probably mistake:
QListWidgetItem* item = ui->myList->itemAt(0,index.row());
Method "itemAt" takes x and y coordinates, not indexes. Use "takeItem" instead.
Next thing I want to say is that this part:
ui->myList->itemWidget(item)
is useless. You can convert "item" directly.
And last - use qobject_cast since you use Qt. And never use dynamic_case (especially when you anyway do not check result against NULL).

check if the name already exists in QTableWidget

I have a problem.
I have 2 QTextEdit fields : value & name.
When I push the button i create QTableWidgetItems with the value from "value" and "name".
But now I will check if the name alredy exists.
But I don't know, with " findItems " ? with contain's ?
Tabelle extends from QWidget in the header.
I'am an c++/ QT Beginner and have no idea as I do that such.
PS: I'am speaking Germany, so you can answer in Germany, my English isn't very good ;D
Thank you :)
void Tabelle::pushButtonClicked() :
strname = ( txtname ->text ());
strvalue = ( txtvalue ->text ());
The textfields to Strings.
Put the vlaue in Items:
QTableWidgetItem * valueitem = new QTableWidgetItem(0);
valueitem->setText(strvalue);
QTableWidgetItem * nameitem = new QTableWidgetItem(0);
nameitem->setText(strname);
New row :
if ( cou >coucount )
{table->insertRow(table->rowCount());}
table->setItem( cou,1, valueitem );
table->setItem( cou, 0, nameitem); cou++
You can use QList QTableWidget::findItems(const QString & text, Qt::MatchFlags flags) const.
As the doc says:
Finds items that matches the text using the given flags.
Try the following code:
QList<QTableWidgetItem *> ItemList = Table->findItems("TestName", Qt::MatchExactly);
cout<< "Count:" << ItemList.count() << endl;

QTableWidget::itemAt() returns seemingly random items

I've just started using Qt, so please bear with me. When I use QTableWidget->getItemAt(), it returns a different item from if I used currentItemChanged and clicked the same item. I believe it's necessary to use itemAt() since I need to get the first column of whatever row was clicked.
Some example code is below:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QList<QString> rowContents;
rowContents << "Foo" << "Bar" << "Baz" << "Qux" << "Quux" << "Corge" << "Grault" << "Garply" << "Waldo" << "Fred";
for(int i =0; i < 10; ++i)
{
ui->tableTest->insertRow(i);
ui->tableTest->setItem(i, 0, new QTableWidgetItem(rowContents[i]));
ui->tableTest->setItem(i, 1, new QTableWidgetItem(QString::number(i)));
}
}
//...
void MainWindow::on_tableTest_currentItemChanged(QTableWidgetItem* current, QTableWidgetItem* previous)
{
ui->lblColumn->setText(QString::number(current->column()));
ui->lblRow->setText(QString::number(current->row()));
ui->lblCurrentItem->setText(current->text());
ui->lblCurrentCell->setText(ui->tableTest->itemAt(current->row(), current->column())->text());
}
For the item at 1x9, lblCurrentItem displays "9" (as it should,) whereas lblCurrentCell displays "Quux". Am I doing something wrong?
Qt documenration says:
QTableWidgetItem * QTableWidget::itemAt ( int ax, int ay ) const
Returns the item at the position equivalent to QPoint(ax, ay) in the table widget's coordinate system, or returns 0 if the specified point is not covered by an item in the table widget.
See also item().
So you should probably use item(row, column) instead:
ui->lblCurrentCell->setText(ui->tableTest->item(current->row(), current->column())->text());
Looks like your table is getting sorted as per the 0th ("Foo, Bar, ...") column. That way 'Q'uux being at 9, before Waldo makes sense. Either insert the numbers at 0th column or disable sorting or I think you got the point. There are many solutions.