Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QAbstractTableModel, SetData, focus
Forum Updated to NodeBB v4.3 + New Features

QAbstractTableModel, SetData, focus

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 1.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Slawomir_Piernikowski
    wrote on last edited by aha_1980
    #1

    Hi
    See hereunder 3 photos:
    0_1550915804056_First action.png
    0_1550915834044_Action 2.png
    0_1550915846462_Action 3.png
    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.

    JonBJ 1 Reply Last reply
    0
    • S Slawomir_Piernikowski

      Hi
      See hereunder 3 photos:
      0_1550915804056_First action.png
      0_1550915834044_Action 2.png
      0_1550915846462_Action 3.png
      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.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Slawomir_Piernikowski
      Sorry I don't understand your question, but I just wanted to say that the poodle-picture is cute :)

      1 Reply Last reply
      1
      • S Offline
        S Offline
        Slawomir_Piernikowski
        wrote on last edited by
        #3

        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:

        1. 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:
        2. 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).

        1 Reply Last reply
        0
        • Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Are you looking for QAbstractItemDelegate::commitData ?

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          S 1 Reply Last reply
          3
          • Christian EhrlicherC Christian Ehrlicher

            Are you looking for QAbstractItemDelegate::commitData ?

            S Offline
            S Offline
            Slawomir_Piernikowski
            wrote on last edited by Slawomir_Piernikowski
            #5

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

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved