check if the name already exists in QTableWidget - c++

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;

Related

QWidget findChildren when objectname contains a specific string

How do you list all childs from a qwidget that contain a specific string inside de objectname?
For example, if I have:
"general_widget", with children:
"label_name_1"
"label__1"
"label_name_2"
"label_id_2"
"label_name_3"
"label_id_3"
"label_name_4"
"label_id_4"
I would like to get a list of all children that contain "name" as part of the objectName, and another list with all children that contain "id". Thanks!
Have a simple function like this:
QList<QWidget *> widgets(QWidget * parent, QString search)
{
QRegularExpression exp(search);
return parent->findChildren<QWidget *>(exp);
}
and given a QWidget * widget you can call it this way:
auto name_list = widgets(widget, "name");
auto id_list = widgets(widget, "id");
Use findChildren() along with objectName().contains() , for example:
QList<QWidget*> subwidgets = this->findChildren<QWidget*>();
QListIterator<QWidget*> it(subwidgets); // iterate through the list of widgets
QWidget *awiget;
while (it.hasNext()) {
awiget = it.next(); // take each widget in the list
if (awiget->objectName().contains("name")){
qDebug() << awiget->objectName();
}
}

Adding text to existing item of QListWidget

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.

duplicate rows in QStandardItemModel in Qt

I'm trying to achieve something I though would be easy but cannot figure how to make it work.
In this basic example, I'm creating an address book, a single person can be in 2 groups, John Doe is a friend, but also a work colleague.
If I change his phoneNumber inside the friend group, it should also change in the work group.
This is how I first tried this with static content (in the end it is linked to a db)
addressBookListModel = new QStandardItemModel(0, 4);
addressBookListModel->setHeaderData(0,Qt::Horizontal,"First Name", Qt::DisplayRole);
addressBookListModel->setHeaderData(0,Qt::Horizontal,"fn", Qt::UserRole);
addressBookListModel->setHeaderData(1,Qt::Horizontal,"Last Name", Qt::DisplayRole);
addressBookListModel->setHeaderData(1,Qt::Horizontal,"ln", Qt::UserRole);
addressBookListModel->setHeaderData(2,Qt::Horizontal,"E-Mail", Qt::DisplayRole);
addressBookListModel->setHeaderData(2,Qt::Horizontal,"mail", Qt::UserRole);
addressBookListModel->setHeaderData(3,Qt::Horizontal,"Phone Number", Qt::DisplayRole);
addressBookListModel->setHeaderData(3,Qt::Horizontal,"phone", Qt::UserRole);
Then inserting the data :
//Group 1
QStandardItem * work = new QStandardItem("Work");
QList<QStandardItem*> workgroup;
workgroup << work ;
addressBookListModel->appendRow(workgroup);
//group 2
QStandardItem * friends = new QStandardItem("Friends");
QList<QStandardItem*> friendgroup;
friendgroup << friends ;
addressBookListModel->appendRow(friendgroup);
//One contact in both groups
QStandardItem * fn = new QStandardItem("John");
QStandardItem * ln = new QStandardItem("Doe");
QStandardItem * mail = new QStandardItem("john.doe#gmail.com");
QStandardItem * phone = new QStandardItem("+123456789");
QList<QStandardItem*> rowitems;
rowitems << fn << ln << mail << phone;
work->appendRow(rowitems);
friends->appendRow(row items);
but this only inserts john doe inside work like this :
I Thought this would be because row items is not a pointer, so I tried it this way :
//One contact in both groups 2
QStandardItem * fn = new QStandardItem("John");
QStandardItem * ln = new QStandardItem("Doe");
QStandardItem * mail = new QStandardItem("john.doe#gmail.com");
QStandardItem * phone = new QStandardItem("+123456789");
QList<QStandardItem*> rowitems;
rowitems << fn << ln << mail << phone;
QList<QStandardItem*> rowitemsB;
rowitemsB << fn << ln << mail << phone;
work->appendRow(rowitems);
friends->appendRow(rowitemsB);
But this gave me the exact same result, John Doe is not present inside Friends, although in both cases, there is an arrow indicating the presence of a child.
Any Idea how to make the same data appear twice?
A QStandardItem can be added once and only once to a QStandardItemModel. Check your debugger log, I'm sure QT writes debug messages saying it's not allowed when you insert the same item twice. By the way, what would return QStandrardItem::index() of an item inserted twice (this method returns row/column position of the item in the QStandardItemModel)?
So you have to create different QStandardItem. You should do like that:
void addEntry( const QString& first, const QString& last, const QString& mail, const QString& tel, QStandardItem* parent )
{
QStandardItem * fn = new QStandardItem(first);
QStandardItem * ln = new QStandardItem(last);
QStandardItem * mail = new QStandardItem(mail);
QStandardItem * phone = new QStandardItem(tel);
QList<QStandardItem*> rowitems;
rowitems << fn << ln << mail << phone;
parent->appendRow(rowitems);
}
...
addEntry( "John", "Doe", "john.doe#gmail.com", "+123456789", work );
addEntry( "John", "Doe", "john.doe#gmail.com", "+123456789", friends );

QT ComboBox ItemData from Database

I have the following code which links a QT QComboBox to my sqlite database and it works great.
However in my database I have the Category and Item table linked with a foreign key.
So when I pull info from the QComboBox I need to get the Category_ID not the name which is listed in the box.
How would I go about setting the QComboBox ItemData to the Category_ID field with a model or better yet use the model to set the QComboBox ItemData as my Category Object?
Thanks,
void MainWindow::populatCat()
{
QSqlQueryModel *model = new QSqlQueryModel();
QString sql;
sql = "select Category_Name From Category ORDER BY Category_Name ASC;";
QSqlQuery* query = new QSqlQuery(db);
query->prepare(sql);
if(!query->exec()){
qDebug () << "Query Erorr: " << query->lastError();
}else{
qDebug () << "Query Successful: " << query->lastQuery();
}
model->setQuery(*query);
ui->cboCat->setModel(model);
}
Okay, I'll present an answer now. :)
QComboBox* myBox = new QComboBox();
connect( myBox, SIGNAL( indexChanged( int ) ), this, SLOT( handleIndexChange( int ) ) );
void myObject::handleIndexChange( int /*index*/ ) {
// We actually don't need the index
QComboBox* box = qobject_cast<QComboBox*>( sender() );
if ( box ) {
QVariant data = box->currentData(); // do whatever w/ data... sounds like call toInt() in your case.
}
}
The essence of all three of my approaches is that you have to do something extra to get data() which corresponds to the current item after a change. It would be nice if it emitted a signal taking the underlying data as the argument, but that could get expensive.

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).