QAbstractTableModel, SetData, focus
-
Hi
See hereunder 3 photos:
I would like to update model data just after button open(Otwórz) of the file dialog was clicked.
This means that I would not have to click "the other cell of the table" so that to call the SetData function.
I add that QFile Dialog is created in customized QItemDelegate class preciselly in overrided createEditor function:QWidget *CustomItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const { ....... else if(index.column() == 19) { QLineEdit *line = new QLineEdit(parent); line->setAlignment(Qt::AlignCenter); line->setStyleSheet("color: black;" "background-color: yellow;" "selection-color: yellow;" "selection-background-color: green;"); //placed hereunder code here not in the setEditor data for it works. Purpose of doing so was to avoid double appearing QFileDialog window when setting picture for column 19 line->setText( picturePath = QFileDialog::getOpenFileName(line, "Pobierz zdjęcie.", "./", "Images (*.png *.jpg *.gif)")); return line; } ....... }
Any ideas are wellcommed.
-
@Slawomir_Piernikowski
Sorry I don't understand your question, but I just wanted to say that the poodle-picture is cute :) -
This is my dog (race Maltese)
I tried my best to explain my question but maybe it can be still difficult so I try to explain what is about:I have customized QAbstractTableModel, QTableView, customized QItemDelegate in my soft.
So when you doubleclick on a cell of the tableView you edit its content you can change it(the content by writting something using keyboard) then you click on the other area of the TableView and in this moment the setData function is called. This function is located in your customized QAbstractTableModel and it is overrided (so the body of the function is written by you).
Hereabove is a normal behaviour when setting data in tablesViews.
I wat to change this behaviour a little bit. What I have changed so far is:- when doubleclicking on the cell in a column called "Zdięcie" of the tableView QFiledialog apears and after choosing an image file the path of the file is transffered to the cell (which earlier was clicked and triggered apearing of the QFileDialog)
What I have not change so far is: - I still have to call SetData function(above is writtten of the function) by clicking other area of the tableView so that the cell which was clicked to call the fille dialog lose focus.
I want to change this behaviour somehow. I want to call SetData function without having to click on the other area of the tableview.
So one solution I was thinking about can be for example do the clicking on the other area of the tableView programmatically (not do this by your hand and mouse). - when doubleclicking on the cell in a column called "Zdięcie" of the tableView QFiledialog apears and after choosing an image file the path of the file is transffered to the cell (which earlier was clicked and triggered apearing of the QFileDialog)
-
Are you looking for QAbstractItemDelegate::commitData ?
-
@Christian-Ehrlicher
Sorry for my so late answer but it took me a while to implement your idea. Unfortunetlly
it have'nt helped, in other words did not fix what I had wanted to achieve.
So after clicking the "Otwórz"(open) button of the QFileDialog I had to click on the other area of the table view in order to setData to the model. Simply saying to call the setData function of the customized QAbstractTableModel.
I wanted to avoid the clicking on "the other area of the tableView" with my mouse phisically so I got the idea to do it programmatically.
Pressing Tab key gives the same effect as clicking - both call the setData function.
So I implemented the tab press and release key programmatically and now I have
what I wanted.void CustomItemDelegate::KeyPressProgrammatically() const { INPUT ip; ip.type = INPUT_KEYBOARD; ip.ki.wScan = 0;//hardware scan code for key ip.ki.time = 0; ip.ki.dwExtraInfo = 0; //Press the "Tab" key ip.ki.wVk = 0x09; //VK_TAB ip.ki.dwFlags = 0; //0 for key press SendInput(1, &ip, sizeof(INPUT)); //Relase the "TAB" key ip.ki.dwFlags = KEYEVENTF_KEYUP; SendInput(1, &ip, sizeof(INPUT)); }
QWidget *CustomItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const { ....... else if(index.column() == 19) { QLineEdit *line = new QLineEdit(parent); line->setAlignment(Qt::AlignCenter); line->setStyleSheet("color: black;" "background-color: yellow;" "selection-color: yellow;" "selection-background-color: green;"); //placed hereunder code here not in the setEditor data for it works. Purpose of doing so was to avoid double appearing QFileDialog window when setting picture for column 19 line->setText( picturePath = QFileDialog::getOpenFileName(line, "Pobierz zdjęcie.", "./", "Images (*.png *.jpg *.gif)")); KeyPressProgrammatically(); return line;
I have done some improvments see code hereunder:
void CustomItemDelegate::KeyPressProgrammatically() const { INPUT ip;//Input for Tab key ip.type = INPUT_KEYBOARD; ip.ki.wScan = 0;//hardware scan code for key ip.ki.time = 0; ip.ki.dwExtraInfo = 0; INPUT ip1; //Input for Shift key ip1.type = INPUT_KEYBOARD; ip1.ki.wScan = 0;//hardware scan code for key ip1.ki.time = 0; ip1.ki.dwExtraInfo = 0; //Press the "Shift" key ip1.ki.wVk = 0xA0; ip1.ki.dwFlags = 0; SendInput(1, &ip1, sizeof (INPUT)); //Press the "Tab" key ip.ki.wVk = 0x09; //VK_TAB ip.ki.dwFlags = 0; //0 for key press SendInput(1, &ip, sizeof(INPUT)); //Press the shift and tab key = BackTab //Relase the "TAB" key ip.ki.dwFlags = KEYEVENTF_KEYUP; SendInput(1, &ip, sizeof(INPUT)); //Release the "Shift" key ip1.ki.dwFlags = KEYEVENTF_KEYUP; SendInput(1, &ip1, sizeof (INPUT)); //Press again the Tab key causes to see just have updated picture. ip.ki.wVk = 0x09; //VK_TAB ip.ki.dwFlags = 0; //0 for key press SendInput(1, &ip, sizeof(INPUT)); //Relase the "TAB" key ip.ki.dwFlags = KEYEVENTF_KEYUP; SendInput(1, &ip, sizeof(INPUT)); }
First is triggered BackTab key press (Shift + Tab) then release the both and after tab key press and release.
Now the effect is more satisfactory namely when I click "Otwórz "(open) button of the QFileDialog I can see updated picture at once.