Problems with QTreeWidget - c++

Good morning, I am learning about programming and GUI in Qt c++ and I have some doubts:
Let's suppose you have this GUI shown in the picture. No worries about how has it been created. It has:
QTreeWidget
QLineEdit
QPushButton1
QPushButton2
How would you make the following things?
When clicking on a name or in a value, to make that only the value of this row appear in the QLineEdit
In the next example, I have been able to show the name or the value, but I only want the value to be shown, even if the name has been clicked.
void MainWindow::on_treeView_activated(const QModelIndex &index)
{
QString val = ui->treeView->model()->data(index).toString();
ui->lineEdit->setText(val);
}
If I change the value of the QLineEdit and then click the QPushButton1, the QTreeWidget should be updated with this new value.
If I click the QPushButton2, save "the names = the values" into a .txt file
(FUTURE) I want to have these 3 point clear first. Then, my goal is to make the same with this picture:
Thanks a lot in advance!

Related

Changing qt5 tab names dynamically

Say I have a tabwidget in my ui file
this is how im adding tabs right now:
QPlainTextEdit *tab = new QPlaintextEdit;
int index = ui->tabWidget->addTab(tab, "changeme");
Now I'm wondering if it's possible to change the name of the tab on the go,
for example when subclassing QPLainTextEdit in a class and connecting a signal to it when the text changes then i'd like to add a little star to the tab to indicate that the file has been modified, is it even possible?
QTabWidet::setTabText does what you want.
E.g:
ui->tabWidget->setTabText(index, "new text");

Open a new window when a particular cell of a row in a QTableView is clicked in Qt

I have a table which displays some dynamic data. I need to implement a feature whereby if the user clicks a designated column of the row (note that this particular column doesn't need to display any data. It is simply needs to function as a place to receive this special user input (a click is what I have in mind)), I should be able to open a new window. Note that this new window DOES not need to edit the contents of the table at all.. In fact it will display some other contextual data which is not present in the table itself.
How do I implement this in Qt?
Use Signals and slots. Hope this will be helpful!
connect(tableView,SIGNAL(clicked(const QModelIndex &)),this,SLOT(function(const QModelIndex &)));
void function(const QModelIndex &index)
{
int row=index.row();
int column=index.column();
dialog->show();
//do the stuff
}

Making only one column of QTreeWidget editable // troubleshooting

Mind that this question isn't a duplicate of question Making only one column of a QTreeWidgetItem editable, as it's proposed solution doesn't work.
Hello, so I just want to make only ONE column of my treeWidget editable.
propertyItems.push_back(new QTreeWidgetItem); //gets filled by the while-loop
propertyItems[propertyItems.size()-1]->setText(0, prop.name); //sets the text of the item
propertyItems[propertyItems.size()-1]->setText(1, prop.value);//set the text of the other item
propertyItems[propertyItems.size()-1]->setFlags(Qt::ItemIsEditable);
ui->treeWidget_3->insertTopLevelItem(ui->treeWidget_3->topLevelItemCount(), propertyItems[propertyItems.size()-1]); //appends the items
counter ++;
and
void MainWindow::onTreeWidget3ItemDoubleClicked()
{
if (ui->treeWidget_3->currentColumn() == 2) {
ui->treeWidget_3->editItem(ui->treeWidget_3->currentItem(), ui->treeWidget_3->currentColumn());
}
}
is my approach. ontreeWidget3ItemDoubleClicked is connected with treeWidget::doubleClicked, treeWidget_3 has NO edit-triggers
BUT: when I execute the programm, the QTreeView is just grayed out.
That said, I also tried
propertyItems[propertyItems.size()-1]->setFlags(propertyItems[propertyItem.size()].flags | Qt::ItemIsEditable);
The treeWidget_3 isn't grayed off anymore, but it is still uneditable...
How can I fix this?
BTW: I am a newb to Qt so I might have forgotten something crucial. Sorry in this case.
As mentioned in the documentation:
The QTreeWidgetItem class provides an item for use with the QTreeWidget convenience class.
It means that it won't work for all use cases. The solution is to create your own model and overload the flags(const QModelIndex& index) method returning the appropriate values (basically Qt:: ItemIsEnabled for read-only columns and Qt:: ItemIsEnabled | Qt::ItemIsEditable for the editable one). You can get the column from index.column().
Qt provides an example to start with trees and models.

Disable file name box in QFileDialog

I used a QFileDialog to open a browser.
Here is my code:
QString filePath = QFileDialog::getSaveFileName(this,
"Export Xml", "PluginPythonQt",
"Xml files (*.xml)");
When excute it will show a dialog like this:
I want to disable the "File name:" box in the picture or prevent user to enter a new name. How can i do that ? Thanks.
I believe you can't achieve this — save dialog is about choosing name besides the choosing where to save it. Of course, you might just ignore what user typed and force your name when he hits OK but it will just make the user angry.
Better way, in my opinion, is to use QFileDialog::getExistingDirectory which will allow the user to choose where to save the file but won't allow him to choose the file name. It will be fair, at least.
Similar question was answered in https://forum.qt.io/topic/73973/qfiledialog-with-no-edit-box.
In general, you can hide any element in any widget if you dig a bit into widget's source code to find element's name, when you have a name, you can find the corresponding element via findChild<QWidget *>(elementName).
Usually if you check QSomeWidget.h (Qt is open source!) you can find element names very easily as they are typically listed as the widgets members.
To hide both labels, fileEdit, ComboBox and even buttons, you can use this code:
QFileDialog fileDialog = new QFileDialog;
QWidget * fileNameEdit = fileDialog->findChild<QWidget *>("fileNameEdit");
Q_ASSERT(fileNameEdit);
fileNameEdit->setVisible(false);
QWidget * fileNameLabel = fileDialog->findChild<QWidget *>("fileNameLabel");
fileNameLabel->setVisible(false);
QWidget * fileTypeCombo = fileDialog->findChild<QWidget *>("fileTypeCombo");
Q_ASSERT(fileTypeCombo);
fileTypeCombo->setVisible(false);
QWidget * fileTypeLabel = fileDialog->findChild<QWidget *>("fileTypeLabel");
fileTypeLabel->setVisible(false);
QWidget * fileButtonBox = fileDialog->findChild<QWidget *>("buttonBox");
fileButtonBox->setVisible(false);
Note that even though buttons are hidden, typing Enter on keyboard (or double clicking) would trigger Open button, and dialog might disappear if you haven't done anything in Accept method. So it would also be a good idea to handle state of that button as well if you really wish buttons to be hidden as well.

Setting the Height of a QPlainTextEdit Delegate in a QTableView

I'm working here on a project and currently I'm stuck on the following problem.
It is about a QTableView which has a column called "Description", the cells of this column contain a QPlainTextEditDelegate. I'm failing on setting the Height of the QPlainTextEdit everytime it is entered. Right now it behaves like a QLineEdit until I drag the row ( in which I'm active at that time ) of the QTableView larger.
What I want to do is to change the Height of the QPlainTextEdit once I entered it.
What are your suggestions? How can I proceed to get this thing done?
Thank you all in advance!
BTW Sorry for my poor english :/
edit:
Ok I solved it, but without sizeHint, I used updateEditorGeometry :
void updateEditorGeometry( QWidget* editor, const QStyleOptionViewItem & option, const QModelIndex & index ) const;
And inside this Method, you can set the width or height like you want
editor->setGeometry(option.rect.x(),option.rect.y(),<your_width>,<your_height>);
But thank you anyway!
You should reimplement QAbstractItemDelegate::sizeHint method to return expected height when you create your editor. I don't think that it's necesary to emit QAbstractItemDelegate::sizeHintChanged signal after creating editor, but documentation doesn't say anything. If it doesn't work without it, you should emit sizeHintChanged after returning created editor widget to notify view of need to change row height.