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. ctrl-c does not work for cell containing a number in QTableView
Forum Updated to NodeBB v4.3 + New Features

ctrl-c does not work for cell containing a number in QTableView

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 3 Posters 1.7k Views 2 Watching
  • 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #2

    Hi,

    Please give more details about your setup.

    A minimal compilable example would also be good to test your issue.

    Interested in AI ? www.idiap.ch
    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

    1 Reply Last reply
    3
    • Seb TurS Offline
      Seb TurS Offline
      Seb Tur
      wrote on last edited by Seb Tur
      #3

      I display the values by :

      model_zlc = new QSqlQueryModel;
      
      model_zlc->setQuery( "SELECT * FROM Table ");
      
      model_zlc->setHeaderData(1, Qt::Horizontal, tr("s"));
      model_zlc->setHeaderData(2, Qt::Horizontal, tr("r"));
      model_zlc->setHeaderData(3, Qt::Horizontal, tr("n"));
      model_zlc->setHeaderData(5, Qt::Horizontal, tr("Twr"));
      model_zlc->setHeaderData(4, Qt::Horizontal, tr("Kod"));
      
      proxy_model_zlc=new QSortFilterProxyModel(model_zlc);
      proxy_model_zlc->setSourceModel(model_zlc);
      
      ui->TV->setModel(proxy_model_zlc);
      

      Then I click on any element from column "Kod" and by pressing ctrl-c i get cell's value in the clipboard.
      When I do the same with any alement from "n" column - nothing gets added to clipboard after pressing ctrl-c

      1804c6f6-526a-49e1-9f71-5a58bdecc387-image.png

      I spotted the pattern that copy fails on numeric values

      JonBJ 1 Reply Last reply
      0
      • Seb TurS Seb Tur

        I display the values by :

        model_zlc = new QSqlQueryModel;
        
        model_zlc->setQuery( "SELECT * FROM Table ");
        
        model_zlc->setHeaderData(1, Qt::Horizontal, tr("s"));
        model_zlc->setHeaderData(2, Qt::Horizontal, tr("r"));
        model_zlc->setHeaderData(3, Qt::Horizontal, tr("n"));
        model_zlc->setHeaderData(5, Qt::Horizontal, tr("Twr"));
        model_zlc->setHeaderData(4, Qt::Horizontal, tr("Kod"));
        
        proxy_model_zlc=new QSortFilterProxyModel(model_zlc);
        proxy_model_zlc->setSourceModel(model_zlc);
        
        ui->TV->setModel(proxy_model_zlc);
        

        Then I click on any element from column "Kod" and by pressing ctrl-c i get cell's value in the clipboard.
        When I do the same with any alement from "n" column - nothing gets added to clipboard after pressing ctrl-c

        1804c6f6-526a-49e1-9f71-5a58bdecc387-image.png

        I spotted the pattern that copy fails on numeric values

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

        @Seb-Tur
        This seems odd behaviour. Should be copyable, without you having to do anything.

        The above is all your code? In particular, you don't override the flags() method of either the source or proxy model?

        Does your non-copyable behaviour apply to s and r columns too?

        You might like to state what version of Qt you're on, and what platform?

        EDIT Maybe I'm wrong, and items are not automatically copyable. I'm surprised then that some are and some are not. Anyway, here's a very old post from 2006 (things don't change much in Qt!), https://www.qtcentre.org/threads/1835-Copying-contents-of-QTableView-cell-to-clipboard

        Override/catch keyPressEvent for the table view, check for appropriate key combination (Qt::ControlModifier, Qt::Key_C) , and use something like:

        QApplication::clipboard()->setText(tableView->currentIndex().data().toString())
        
        1 Reply Last reply
        1
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #5

          Beside the point from @JonB and seeing your query, why not use a QSqlTableModel directly ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • Seb TurS Offline
            Seb TurS Offline
            Seb Tur
            wrote on last edited by
            #6

            @JonB
            Qt version 5.9.8 linux also 5.15 linux also 5.9.8 windows 32b
            columns "s" and "r" contain numbers so copying also fails for that ones.

            I will probably need to use QApplication::clipboard()->setText but I'm surprised default behavior is so strange

            I don't use any flags - no other operations to QSqlModel nor QTableView other than the ones stated above - setting query, model and then passing it via proxy to QTableView.

            @SGaist
            I did not know QSqlTableModel existed :) I will try to use it in future

            1 Reply Last reply
            0
            • Seb TurS Offline
              Seb TurS Offline
              Seb Tur
              wrote on last edited by
              #7

              I also noticed ctrl-c fails on cells containing dates

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #8

                Can you provide a minimal compilable example that shows that behaviour ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • Seb TurS Offline
                  Seb TurS Offline
                  Seb Tur
                  wrote on last edited by Seb Tur
                  #9

                  Sure , i created a simple example - copy fails for both direct and via-proxy model setting when selecting cells from columns other than "name" which is the only text column

                  mainwindow.cpp

                  #include "mainwindow.h"
                  #include "ui_mainwindow.h"
                  #include "QDir"
                  
                  
                  
                  MainWindow::MainWindow(QWidget *parent)
                      : QMainWindow(parent)
                      , ui(new Ui::MainWindow)
                  {
                      ui->setupUi(this);
                  
                  }
                  
                  MainWindow::~MainWindow()
                  {
                      delete ui;
                  }
                  
                  
                  void MainWindow::on_PB_sql_connect_clicked()
                  {
                      QString path = QDir::homePath()+"/sqlite.db";
                      QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");//not dbConnection
                      db.setDatabaseName(path);
                      bool ok = db.open();
                      ui->L_status->setText(ok ?"DB connected" : "DB not connected");
                      QSqlQuery query;
                  }
                  
                  void MainWindow::on_PB_sql_table_clicked()
                  {
                      QSqlQuery add;
                      add.exec("CREATE TABLE testtable ( "
                                  " id integer primary key, "
                                  " name varchar(100) , "
                                  " age integer , "
                                  " date date )");
                  
                      QSqlQuery insert;
                      insert.exec(" INSERT INTO testtable VALUES(1, 'all', 10, '2020-04-21'); ");
                      insert.exec(" INSERT INTO testtable VALUES(2, 'text', 22, '2020-04-22'); ");
                      insert.exec(" INSERT INTO testtable VALUES(3, 'copies', 33, '2020-04-23');");
                  
                  }
                  
                  void MainWindow::on_PB_sql_read_clicked()
                  {
                      QSqlQueryModel *model = new QSqlQueryModel;
                      model->setQuery("select * from testtable");
                  //    model->query().exec();
                  
                  
                      if (ui->CB_proxy->isChecked())
                      {
                          QSortFilterProxyModel *proxymodel = new QSortFilterProxyModel;
                          proxymodel->setSourceModel(model);
                          ui->TV->setModel(proxymodel);
                      }
                      else
                      {
                          ui->TV->setModel(model);
                      }
                  
                  
                  }
                  
                  

                  mainwindow.h

                  #ifndef MAINWINDOW_H
                  #define MAINWINDOW_H
                  
                  #include <QMainWindow>
                  #include <QtSql/QSqlDatabase>
                  #include <QtSql/QSqlQuery>
                  #include <QtSql/QSqlQueryModel>
                  #include <QSortFilterProxyModel>
                  
                  QT_BEGIN_NAMESPACE
                  namespace Ui { class MainWindow; }
                  QT_END_NAMESPACE
                  
                  class MainWindow : public QMainWindow
                  {
                      Q_OBJECT
                  
                  public:
                      MainWindow(QWidget *parent = nullptr);
                      ~MainWindow();
                  
                  private slots:
                      void on_PB_sql_connect_clicked();
                  
                      void on_PB_sql_table_clicked();
                  
                      void on_PB_sql_read_clicked();
                  
                  private:
                      Ui::MainWindow *ui;
                      QSqlDatabase db;
                  };
                  #endif // MAINWINDOW_H
                  
                  

                  .pro

                  QT       += core gui sql
                  
                  greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
                  
                  CONFIG += c++11
                  
                  # The following define makes your compiler emit warnings if you use
                  # any Qt feature that has been marked deprecated (the exact warnings
                  # depend on your compiler). Please consult the documentation of the
                  # deprecated API in order to know how to port your code away from it.
                  DEFINES += QT_DEPRECATED_WARNINGS
                  
                  # You can also make your code fail to compile if it uses deprecated APIs.
                  # In order to do so, uncomment the following line.
                  # You can also select to disable deprecated APIs only up to a certain version of Qt.
                  #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
                  
                  SOURCES += \
                      main.cpp \
                      mainwindow.cpp
                  
                  HEADERS += \
                      mainwindow.h
                  
                  FORMS += \
                      mainwindow.ui
                  
                  # Default rules for deployment.
                  qnx: target.path = /tmp/$${TARGET}/bin
                  else: unix:!android: target.path = /opt/$${TARGET}/bin
                  !isEmpty(target.path): INSTALLS += target
                  
                  

                  .ui

                  <?xml version="1.0" encoding="UTF-8"?>
                  <ui version="4.0">
                   <class>MainWindow</class>
                   <widget class="QMainWindow" name="MainWindow">
                    <property name="geometry">
                     <rect>
                      <x>0</x>
                      <y>0</y>
                      <width>954</width>
                      <height>805</height>
                     </rect>
                    </property>
                    <property name="windowTitle">
                     <string>MainWindow</string>
                    </property>
                    <widget class="QWidget" name="centralwidget">
                     <layout class="QVBoxLayout" name="verticalLayout">
                      <item>
                       <widget class="QPushButton" name="PB_sql_connect">
                        <property name="text">
                         <string>Connect db</string>
                        </property>
                       </widget>
                      </item>
                      <item>
                       <widget class="QPushButton" name="PB_sql_table">
                        <property name="text">
                         <string>Create testtable</string>
                        </property>
                       </widget>
                      </item>
                      <item>
                       <widget class="QPushButton" name="PB_sql_read">
                        <property name="text">
                         <string>Output test table</string>
                        </property>
                       </widget>
                      </item>
                      <item>
                       <widget class="QCheckBox" name="CB_proxy">
                        <property name="text">
                         <string>set model via proxy</string>
                        </property>
                       </widget>
                      </item>
                      <item>
                       <widget class="QTableView" name="TV"/>
                      </item>
                      <item>
                       <widget class="QLabel" name="L_status">
                        <property name="text">
                         <string>Status :</string>
                        </property>
                       </widget>
                      </item>
                     </layout>
                    </widget>
                    <widget class="QMenuBar" name="menubar">
                     <property name="geometry">
                      <rect>
                       <x>0</x>
                       <y>0</y>
                       <width>954</width>
                       <height>27</height>
                      </rect>
                     </property>
                    </widget>
                    <widget class="QStatusBar" name="statusbar"/>
                   </widget>
                   <resources/>
                   <connections/>
                  </ui>
                  
                  
                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #10

                    Looks like a limitation in how the item view handles the copy.

                    Please open a feature request on the bug tracker providing that example. Just one thing, please make it work directly with SQLite so it's easier to reproduce your use case as it does not require to have MySQL running.

                    I have an idea on how to fix this.

                    Please post the link to the feature request here.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • Seb TurS Offline
                      Seb TurS Offline
                      Seb Tur
                      wrote on last edited by
                      #11

                      Hi @SGaist
                      I updated the code above.
                      Created an archive with the whole project.
                      created a Bugreport (could not find a "feature request" in a dropdown so selected bug)
                      https://bugreports.qt.io/browse/QTBUG-86166
                      Archive is attached there.

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #12

                        Fix in progress

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        1
                        • Seb TurS Offline
                          Seb TurS Offline
                          Seb Tur
                          wrote on last edited by
                          #13

                          fixed in 5.15.2

                          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