Solved ctrl-c does not work for cell containing a number in QTableView
-
Hi
I'm using a QTableView displaying a sorted QSqlQueryModel via QSortFilterProxyModel.I want to be able to copy paste selected cell value. It works fine with no codding but only for the cells that contain text. When I select a number nothing is copied to clipboard.
Is there any setting that can be applied on the column? Or do I need o subclass tableview and capture keypressed event and write my own method to capture ctr-c ? -
Hi,
Please give more details about your setup.
A minimal compilable example would also be good to test your issue.
-
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-cI spotted the pattern that copy fails on numeric values
-
@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
andr
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())
-
Beside the point from @JonB and seeing your query, why not use a QSqlTableModel directly ?
-
@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 -
I also noticed ctrl-c fails on cells containing dates
-
Can you provide a minimal compilable example that shows that behaviour ?
-
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>
-
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.
-
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. -
Fix in progress
-
fixed in 5.15.2