I have a qtableview and Treeview in my program.
qtableview code is:
void MainWindow::tableShow(QString mystrings)
{
testModel = new QSqlTableModel(this);
testModel->setTable("parts");
if (mystrings=="")
{
testModel->setFilter("");
}
else
{
testModel->setFilter("type='"+mystrings+"'");
}
testModel->select();
ui->tableView->setModel(testModel);
}
it works just fine and the very interesting ability it has is you can edit cells and database updates automatically.
Now I have made a TreeView with this code:
void MainWindow::treeView2()
{
QStandardItemModel *myModel= new QStandardItemModel;
QStandardItem *item = new QStandardItem();
item = myModel->invisibleRootItem();
myQuery.exec("select groupName from maingroup");
while(myQuery.next()){
QString temp =(myQuery.value(0).toString());
QStandardItem *parItem= new QStandardItem(temp);
item->appendRow(parItem);
QSqlQuery childqry;
childqry.exec("select sub from subgroup where main = '"+temp+"'");
while(childqry.next()){
QStandardItem *childItem= new QStandardItem(childqry.value(0).toString());
parItem->appendRow(childItem);
}
}
ui->treeView->setModel(myModel);
ui->treeView->expandAll();
}
but editting item in this treeview do not updates the database and after restarting the program the item is like before. what should i do to automatically update database with new value that edited in treeview?
thanks in advance.
Related
I have a QListview where I have set a model. The model contains the QList. Now I want to get the contents of QListview on clicking the item. But I don't know how to do this? Through some tutorials I followed I am able to get two items appear in the QListview as follows. But I dont know how to make it work?? Please anyone help. The code which I am working as follows.
listviewmodel =new QListView;
listviewmodel->setModel( createModel() );
listviewmodel->setViewMode(QListView::IconMode);
listviewmodel->setIconSize(QSize(size().width()/8, size().height()/8));
connect(listviewmodel,SIGNAL(clicked(const QModelIndex)),this,SLOT(ItemClicked(QModelIndex)));
QAbstractItemModel *MainWindow::createModel()
{
QStandardItemModel *model = new QStandardItemModel();
QList<QStandardItem *> listItem;
QStandardItem *item2 = new QStandardItem();
item2->setIcon(QIcon(QPixmap::fromImage(qimages2)));
listItem << item2;
QStandardItem *item1 = new QStandardItem();
item1->setIcon(QIcon(QPixmap::fromImage(qimages1)));
listItem << item1;
model->appendColumn(listItem);
return model;
}
void MainWindow::ItemClicked (QModelIndex index )
{
textEdit->setText(index.data().toString());
}
I checked your code in QtCreator and it works as you described. So what is the problem? Are you getting some errors? Maybe did you not put declaration of ItemClicked(QModelIndex) below the public slots:? Are you sure that connect returns true? Try to check it:
bool success = connect(listviewmodel,SIGNAL(clicked(const QModelIndex)),this,SLOT(ItemClicked(QModelIndex)));
Q_ASSERT(success);
EDIT: If you want to display only icons at QListView and get some text informations after clicking on specified item, you can do it in the following way:
item2->setIcon(QIcon(QPixmap::fromImage(qimages2)));
item2->setData("informations about item2", Qt::UserRole);
(...)
void MainWindow::ItemClicked (QModelIndex index )
{
QString data = index.data(Qt::UserRole).value<QString>();
ui->textEdit->setText(data);
}
I have create a QTreeView and which is filed by QTreeWidgetItem. My application is a kind of file browser.
ViewTree is defined as below:
void MainWindow::createTreeView()
{
TreeViewSection = new QWidget();
QVBoxLayout *TreeViewLayout = new QVBoxLayout(TreeViewSection);
MyTree = new TreeWidget();
contextMenu = new QMenu(MyTree);
MyTree->setContextMenuPolicy(Qt::ActionsContextMenu);
addFolderAction = new QAction("Add Folder", contextMenu);
MyTree->addAction(addFolderAction);
connect(addFolderAction, SIGNAL(triggered()),this,SLOT(onAddFolderAction()));
deleteAction = new QAction("Delete", contextMenu);
MyTree->addAction(deleteAction);
connect(deleteAction, SIGNAL(triggered()),this,SLOT(onDeleteAction()));
MyTree->setSortingEnabled(true);
MyTree->setColumnWidth(0, 400);
headerItem = new QTreeWidgetItem();
headerItem->setText(0,QString("File Name"));
headerItem->setText(1,QString("Last Modified"));
headerItem->setText(2,QString("Size"));
MyTree->setHeaderItem(headerItem);
MyTree->setAcceptDrops(true);
MyTree->setDragEnabled(true);
MyTree->setDragDropMode(QAbstractItemView::InternalMove);
MyTree->setDefaultDropAction(Qt::MoveAction);
MyTree->setDropIndicatorShown(true);
MyTree->setDragDropOverwriteMode(true);
MyTree->setSelectionMode(QAbstractItemView::SingleSelection);
TreeViewLayout->addWidget(MyTree);
}
I have added an "Add Folder" Button, as defined below for the action:
void MainWindow::onAddFolderAction()
{
QList<QTreeWidgetItem *> item;
uint32_t index;
item = MyTree->selectedItems();
if (item.empty())
index = 0;
else {
QString str = item[0]->text(0);
QByteArray latin_str = str.toLatin1();
char *utf8_text = latin_str.data();
index = m_device.getIdByName(utf8_text);
}
if(m_device.isFolder(index) == true) {
QTreeWidgetItem* child = new QTreeWidgetItem();
child->setText(0, "NewFolder");
child->setText(1, "--");
child->setText(2, "--");
item[0]->addChild(child);
item[0]->setExpanded(true);
//item[0]->setSelected();
// MyTree->edit(selectedItem());
m_device.CreateNewFolder("New Folder", index);
}
}
As you see, by default I create a "NewFolder" which on a mtp FS system.
What I want to do it before creating the folder, I would like to be able to rename "NewFolder" to what the user want. I don't want to create a pop-up to enter text. What I want is a "kind of renaming" behavior. The folder appears in the treeview, the new folder is selected and the text is allowed to be changed directly. Like on Finder when you made a single click on the folder name, it become selected, and user can change the name in the finder without any pop-up
Thanks
If you want to edit the item right after creating it you should be able to achieve this behaviour by simply calling the editor on it:
QTreeWidgetItem* child = new QTreeWidgetItem();
child->setText(0, "NewFolder");
child->setText(1, "--");
child->setText(2, "--");
child->setFlags(child->flags() | Qt::ItemIsEditable);
item[0]->addChild(child);
item[0]->setExpanded(true);
MyTree->editItem(child);
I want to add Qdate to my table say QTableview.The problem is if i convert it into string i can add and retrieve the data.But i want to store as date only in my model.
void MainWindow::setUpTabel()
{
QDateTime myDate;
myDate.setDate(QDate::currentDate());
//myModel
QStandardItemModel model = new QStandardItemModel(this);
QStandardItem *item = new QStandardItem;
item.setData(myDate,Qt::UserRole);
//Myview is also created and set the model to it
m_tableView->setModel(model);
}
The problem is i'm not able to see the date in my table.
As the documentation says, you must set the item into the model specifying the row and columng where you are going to set the item.
http://qt-project.org/doc/qt-4.8/qstandarditemmodel.html
Modifying your code:
void MainWindow::setUpTabel()
{
int row = 0, column = 0; // here you decide where is the item
QDateTime myDate;
myDate.setDate(QDate::currentDate());
QStandardItemModel model = new QStandardItemModel(this);
QStandardItem *item = new QStandardItem(myDate);
model.setItem(row, column, item);
m_tableView->setModel(model);
}
I have a combobox with multiple checkbox items and I need to be able to iterate through them and determine if they are checked or even more appropriately, as soon as an item is checked, have an event fire off but I can't seem to get the signal/slot combo working correctly.
Here is some of my code:
void MainWindow::setupQueryBuilder(Ui::MainWindow* ui)
{
QStandardItemModel* assessmentModel = new QStandardItemModel(3, 1);
QStandardItem* assessmentPleaseSelect = new QStandardItem();
QStandardItem* assessmentVisionScreening = new QStandardItem();
QStandardItem* assessmentHearingAssessment = new QStandardItem();
QStandardItem* assessmentDevelopmentalAssessment = new QStandardItem();
assessmentPleaseSelect->setText("Please Select...");
assessmentPleaseSelect->setFlags(Qt::ItemIsEnabled);
assessmentPleaseSelect->setData(Qt::CheckStateRole);
assessmentVisionScreening->setText("Vision Screening");
assessmentVisionScreening->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
assessmentVisionScreening->setData(Qt::Unchecked, Qt::CheckStateRole);
assessmentHearingAssessment->setText("Hearing Assessment");
assessmentHearingAssessment->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
assessmentHearingAssessment->setData(Qt::Unchecked, Qt::CheckStateRole);
assessmentDevelopmentalAssessment->setText("Developmental Assessment");
assessmentDevelopmentalAssessment->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
assessmentDevelopmentalAssessment->setData(Qt::Unchecked, Qt::CheckStateRole);
assessmentModel->insertRow(0, assessmentPleaseSelect);
assessmentModel->insertRow(1, assessmentVisionScreening);
assessmentModel->insertRow(2, assessmentHearingAssessment);
assessmentModel->insertRow(3, assessmentDevelopmentalAssessment);
ui->SearchAssessmentSelect->setModel(assessmentModel);
}
I've not tried this, but from looking at the docs you have a signal for QStandardItemModel called itemChanged(QStandardItem *item). What you'd want to do is created a slot named (for example) modelItemChanged(QStandardItem *item) and connect them with connect(assessmentModel, SIGNAL(itemChanged(QStandardItem *)), this, SLOT(modelItemChanged(QStandardItem *))).
That should set your model to trigger the slot whenever an QStandardItem is changed, and it'll pass a pointer to the item that was changed. In your modelItemChanged(QStandardItem *item) function you can then query the QStandardItem *item to see what state it is in.
i like to make my QStandardItemModel that populates items in qtreeview
to append rows on top the allready defined items in the view .
something like the twitter view , new items first.
all i see in the QStandardItemModel is the appendRow/s that appends then to button.
this is what im using now.
SWidget *widget = new SWidget;
QStandardItem *newItem = new QStandardItem;
newItem->setSizeHint( widget->size() );
appendRow( newItem );
view->setIndexWidget( newItem->index(), widget );
void QStandardItemModel::insertRow ( int row, QStandardItem * item ) inserts a row at row containing item. So instead of calling appendRow(newItem); call insertRow(0, newItem);