I have a treeWidget in which I need to remove the QTreeWidgetItem's childelement.
QTreeWidgetItem *listing = new (ui->treeWidget);
AddChild(listing,id);
AddChild(listing,id);`// this is what i need to delete
AddChild(listing,id);
listing->takeChild(1);
I tried func takeChild(int index) but nothing happend
void AddChild(QTreeWidget *s,int id)
{
QTreeWidgetItem *_s = new QTreeWidgetItem(ui->treeWidget);
_s->setText(0,QString::number(id));
s->addChild(_s);
}
There are errors in your code,
QTreeWidgetItem *listing = new (ui->treeWidget);
should be:
QTreeWidgetItem *listing = new QTreeWidgetItem(ui->treeWidget);
and the signature of AddChild() functions is wrong:
void AddChild(QTreeWidget *s,int id)
should be:
AddChild(QTreeWidgetItem *s,int id)
Now the main issue why takeChild() is not working for you, is that you are not adding the items as children to listing , but you are adding them to the treeWidget, thus in your AddChild(), you should set the parent to S which is listing, So your code should be:
QTreeWidgetItem *listing = new QTreeWidgetItem(ui->treeWidget);
listing->setText(0, tr("Listing"));
AddChild(listing,0);
AddChild(listing,1); // this is what i need to delete
AddChild(listing,2);
listing->takeChild(1);
and the function AddChild()
void AddChild(QTreeWidgetItem *s,int id)
{
QTreeWidgetItem *_s = new QTreeWidgetItem(s);
_s->setText(0,QString::number(id));
s->addChild(_s);
}
Related
Implemented Tree using QStandardItemModel.. like below
QStandardItem *americaItem = new QStandardItem("America");
QStandardItem *mexicoItem = new QStandardItem("Canada");
QStandardItem *usaItem = new QStandardItem("USA");
QStandardItem *bostonItem = new QStandardItem("Boston");
QStandardItem *europeItem = new QStandardItem("Europe");
QStandardItem *italyItem = new QStandardItem("Italy");
QStandardItem *romeItem = new QStandardItem("Rome");
QStandardItem *veronaItem = new QStandardItem("Verona");
//building up the hierarchy
rootNode-> appendRow(americaItem);
rootNode-> appendRow(europeItem);
americaItem-> appendRow(mexicoItem);
americaItem-> appendRow(usaItem);
usaItem-> appendRow(bostonItem);
europeItem-> appendRow(italyItem);
italyItem-> appendRow(romeItem);
italyItem-> appendRow(veronaItem);
//register the model
treeView->setModel(standardModel);
So now Im unable to do search operation, using that QFilterProxyModel im able to search only parent data.. Any suggestion to search parent and child rows too..(using filterAcceptsRow or any other)
Implemented search filter like this:
Derived a class from QSortFilterProxyModel
Overrided the filterAcceptsRow method
bool FilterModel::filterAcceptsRow(int source_row, const QModelIndex& source_parent) const
{
// check the current item
bool result = QSortFilterProxyModel::filterAcceptsRow(source_row,source_parent);
QModelIndex currntIndex = sourceModel()->index(source_row, 0, source_parent);
if (sourceModel()->hasChildren(currntIndex))
{
// if it has sub items
for (int i = 0; i < sourceModel()->rowCount(currntIndex) && !result; ++i)
{
// keep the parent if a children is shown
result = result || filterAcceptsRow(i, currntIndex);
}
}
return result;
}
mFilterProxyModel->setFilterRegExp(ui->mLeSearchFilter->text());
I have a qtablewidget with one column with a widget and others with data. The only column with widget is shown and all other columns are hidden.
foreach (BillHeader *billHeader, billHeaderList)
{
m_pBillTable->insertRow(i);
itemWidget = new LookupItem;
itemWidget->setImage(1);
...
m_pBillTable->setCellWidget(i, 0, itemWidget);
tableItem = new QTableWidgetItem(billHeader->billNumber);
tableItem->setTextAlignment(Qt::AlignCenter);
m_pBillTable->setItem(i, 1, tableItem);
...
m_pBillTable->hideColumn(1);
...
I have a signal slot connected as below:
connect(m_pOkButton, SIGNAL(clicked()), this, SLOT(handleOkClick()));
when ok button click i try to get the selected item and get data from the widget set to it
void OrderLookup::handleOkClick()
{
qDebug()<<Q_FUNC_INFO<<"Invoked";
QList<QTableWidgetItem*> itemList = m_pBillTable->selectedItems();
qDebug()<<Q_FUNC_INFO<<itemList.count();
if (!itemList.isEmpty())
{
int row = itemList.at(0)->row();
qDebug()<<Q_FUNC_INFO<<row;
LookupItem *item = (LookupItem*)m_pBillTable->cellWidget(row, 0);
if (NULL != item)
{
QString billNumber = item->getBillNumber();
emit orderLookupComplete(billNumber);
accept();
}
}
qDebug()<<Q_FUNC_INFO<<"Exits";
}
But i am getting the list count as zero.
The row is getting selected and gets highlighted.
I’ve set some properties to table widget as below:
m_pBillTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
m_pBillTable->setSelectionBehavior(QAbstractItemView::SelectRows);
m_pBillTable->setSelectionMode(QAbstractItemView::SingleSelection);
m_pBillTable->setFocusPolicy(Qt::NoFocus);
Please can someone help me to know why list count is empty..
The issue got solved ..
QItemSelectionModel *itemModel = m_pBillTable->selectionModel();
QModelIndexList indexList = itemModel->selectedRows();
qDebug()<<Q_FUNC_INFO<<"IndexList Count"<<indexList.count();
if (!indexList.isEmpty())
{
int row = indexList.at(0).row();
Im trying to create a subtree using QTreeWidgetItem.
Here's code
m_pRoot = new QTreeWidgetItem(treeWidget);
m_pRoot->setText(0, "root");
m_pRoot->setData(0, Qt::UserRole, IT_ROOT);
createTreeItem(m_pRoot, m_pText, "subtree_1", IT_TEXT);
m_pRoot->setExpanded(true);
createTreeItem(m_pText, m_pPlainText, "subtree_2", IT_TEXT);
that's method:
void createTreeItem(PTR(QTreeWidgetItem) parent, PTR(QTreeWidgetItem) item, CREF(QString) name, ItemType itemType)
{
item = new QTreeWidgetItem(parent);
item->setText(0, name);
item->setData(0, Qt::UserRole, QVariant(itemType));
}
It's ok when im creating a "subtree_1" with m_pRoot as root item, but it breaks down when i want to create "subtree_2" with m_pText as root item.
P.S PTR() equal *parent.
Cant understand what's the problem. Any suggestions?
If your PTR(QTreeQWidgetItem) macro yields QTreeWidgetItem*, then item is not initialized by createTreeItem function.
The simplest and most logical fix seems to change createTreeItem function to return new item:
QTreeWidgetItem* createTreeItem(PTR(QTreeWidgetItem) parent, PTR(QTreeWidgetItem) item,
CREF(QString) name, ItemType itemType)
{
item = new QTreeWidgetItem(parent);
item->setText(0, name);
item->setData(0, Qt::UserRole, QVariant(itemType));
return item;
}
and use the returned value to initialize m_pText.
editlistofcustomers.h
QSqlRelationalTableModel *RelationalModel;
TreeModel *CategoriesModel;
QSqlRecord Record;
editlistofcustomers.cpp
EditListOfCustomers::EditListOfCustomers // constructor
RelationalModel = new QSqlRelationalTableModel(this, *SupportObj->GetDataBase());
RelationalModel->setTable(SupportObj->GetCustomersTableName());
RelationalModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
RelationalModel->select();
RelationalModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Название/имя покупателя"));
Record = RelationalModel->record();
RelationalModel->setRelation(2, QSqlRelation(SupportObj->GetCategoriesOfCustomersTableName(),
SupportObj->GetCategoriesOfCustomersPattern()[0].GetName(), // ID.
SupportObj->GetCategoriesOfCustomersPattern()[2].GetName())); // Name.
CategoriesModel = new TreeModel(QObject::tr("Категории"),
SupportObj->GetCategoriesOfCustomersTableName(),
SupportObj, this);
//Setup model view delegate.
ui->TV_ListOfCustomers->setModel(RelationalModel);
ui->TV_ListOfCustomers->setItemDelegate(new QSqlRelationalDelegate(ui->TV_ListOfCustomers));
ui->TV_ListOfCustomers->setSelectionBehavior(QAbstractItemView::SelectRows);
ui->TV_ListOfCustomers->setSortingEnabled(true);
ui->TV_CategoryOfCustomer->setModel(CategoriesModel);
SupportObj->GetDataBase()->transaction()
EditListOfCustomers::AddCustomer
QString customerName = ui->LE_CustomerName->text();
if(!customerName.isEmpty())
{
Record.setValue(SupportObj->GetCustomersPattern()[1].GetName(), QVariant(customerName)); // Name.
int categoryID = CategoriesModel->GetItemID(ui->TV_CategoryOfCustomer->currentIndex());
Record.setValue(SupportObj->GetCustomersPattern()[2].GetName(), QVariant(categoryID)); // Category ID.
Record.setValue(SupportObj->GetCustomersPattern()[3].GetName(), QVariant(ui->LE_CustomerTelephoneNumbers->text())); // Telephone numbers.
Record.setValue(SupportObj->GetCustomersPattern()[4].GetName(), QVariant(ui->LE_CustomerAddress->text())); // Address.
Record.setValue(SupportObj->GetCustomersPattern()[5].GetName(), QVariant(ui->TE_CustomerComment->toPlainText())); // Comment.
RelationalModel->insertRecord(-1, Record);
if(!RelationalModel->submitAll())
{
QMessageBox::warning(this, "CategoriesEditor::CategoriesEditor", SupportObj->GetDataBase()->lastError().text());
}
// Clear fields
ui->LE_CustomerName->clear();
ui->TV_CategoryOfCustomer->setCurrentIndex(QModelIndex());
ui->LE_CustomerTelephoneNumbers->clear();
ui->LE_CustomerAddress->clear();
ui->TE_CustomerComment->clear();
ui->LE_CustomerName->setFocus();
ui->TV_ListOfCustomers->sortByColumn(0, Qt::AscendingOrder);
}
If comment "RelationalModel->setRelation( ... )" everything work fine: transaction, adding, deleting, commit or rollback. If uncomment nothing work, but show everything right(ID replaced by category name), but I can't insert new row using "insertRecord". I try this
suggestion, but unsuccessfully.
Any suggestion?
The problem was in the column name. setRelation changes column name category_of_customer_id to category_of_customers_name_2.
The solution use void setValue(int index, const QVariant &val) instead of void setValue(const QString & name, const QVariant &val).
QSqlRecord record = RelationalModel->record();
record.setValue(1, QVariant(customerName)); // Name.
int categoryID = CategoriesModel->GetItemID(ui->TV_CategoryOfCustomer->currentIndex());
record.setValue(2, QVariant(categoryID)); // Category ID.
record.setValue(3, QVariant(ui->LE_CustomerTelephoneNumbers->text())); // Telephone numbers.
record.setValue(4, QVariant(ui->LE_CustomerAddress->text())); // Address.
record.setValue(5, QVariant(ui->TE_CustomerComment->toPlainText())); // Comment.
RelationalModel->insertRecord(-1, record);
if(!RelationalModel->submitAll())
{
QMessageBox::warning(this, "CategoriesEditor::CategoriesEditor", SupportObj->GetDataBase()->lastError().text());
}
Had the same issue. This only needs to happen for the field that has a setRelation. Here's the solution for PyQt:
def insertRow(self):
rec = self.record()
rec.setValue("id", "new id")
rec.setValue("type", 1)
rec.setValue("title", "new title")
return self.insertRecord(-1, rec)
self.submit()
Apologies. Wanted to comment, but don't yet have a reputation of 50+.
I've been beating my head around trying to populate a ListView with data from an SQLite database and can't seem to figure it out. I've read countless tutorials and equally countless posts here, but I'm obviously not getting something critical. Was hoping someone could give me a hint as to why the following two pieces of code aren't working together, or if I should be looking at something else entirely. Any help would be appreciated. The result I'm getting is a force close.
Method that initiates populating ListView object
public void displayNurseRoster(){
listView = (ListView) findViewById(R.id.list);
// create instance of DbCommunicator
DbCommunicator rosterView = new DbCommunicator(this);
// open instance
rosterView.open();
// instantiate SimpleCursorAdapter instance and set value
SimpleCursorAdapter cursorAdapter;
cursorAdapter = rosterView.getRosterListViewAdapter(this);
// close database instance
rosterView.close();
// set adapter to listView
listView.setAdapter(cursorAdapter);
}
Method that returns SimpleCursorAdapter:
public SimpleCursorAdapter getRosterListViewAdapter (Context context) {
// method variables
int[] to = new int[] {R.id.rosterListLname};
// used ArrayList because of unpredictability of array length
List<String> dataArray = new ArrayList<String>();
String[] columns = new String[] {KEY_NURSE_ROWID, KEY_LNAME};
// create cursor
Cursor cursor = sqldb.query(NURSE_TABLE, columns, null, null, null,
null, KEY_LNAME);
int iLname = cursor.getColumnIndex(KEY_LNAME);
cursor.moveToFirst();
String result = "";
while(!cursor.isAfterLast()){
result = result
+ cursor.getString(iLname) + "\n";
dataArray.add(result);
}
// convert ArrayList to String array for use with SimpleCursorAdapter
String [] from = (String[]) dataArray.toArray();
SimpleCursorAdapter adapter = new SimpleCursorAdapter(context,
R.layout.edit_roster, cursor, from, to);
return adapter;
}
Okay, I got it to work and wanted to share to help out anyone else struggling with this and 2) to get feedback on my fix in case anyone with more experience has a better suggestion.
I actually wanted to show three items in the list view, so here's the code that worked. One of the problems I was having was that I was extending ListActivity, but found in another StackOverflow post that that is not a good idea when there is only one list view to worry about.
package com.deadEddie.staffingmanagement;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class EditRoster extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_roster);
displayNurseRoster();
}
public void displayNurseRoster(){
ListView listView = (ListView) findViewById(R.id.list);
int[] to = new int[] {
R.id.rosterListLname,
R.id.rosterListFname,
R.id.rosterListMI };
String[] from = new String [] {
DbCommunicator.KEY_LNAME,
DbCommunicator.KEY_FNAME,
DbCommunicator.KEY_MI};
// create instance of DbCommunicator
DbCommunicator rosterView = new DbCommunicator(this);
// open instance
rosterView.open();
// get & manage cursor
Cursor cursor = rosterView.getRosterCursor(this);
startManagingCursor(cursor);
// instantiate cursor adaptor
ListAdapter adapter = new SimpleCursorAdapter(this,
R.layout.nurse_list, cursor, from, to);
cursor.moveToNext();
// set adapter to listView
listView.setAdapter(adapter);
}// displayNurseRoster()
}
Here's the method in my DbCommunicator class. What finally made the difference was that I was not creating an instance of SQLiteQueryBuilder and using that, but instead I was using sqldb.query(...), as shown above. I'm not sure what the difference is, but it finally did the trick. If anyone would like to share, please do.
public Cursor getRosterCursor (Context context) {
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
queryBuilder.setTables(NURSE_TABLE);
Cursor cursor = queryBuilder.query(sqldb, new String[] {
KEY_NURSE_ROWID,
KEY_LNAME,
KEY_FNAME,
KEY_MI},
null, null, null, null, null);
return cursor;
}
A couple other newbie lessons for anyone else out there:
1. Always use the "_id" field in the cursor. The cursor cannot function without that.
2. The while or for loop is not necessary for a simple list view. The cursor adapter handles that.
3. There is no need to call the moveToFirst() method on the cursor.
Hope that's helpful to someone.