Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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 ?


  • Lifetime Qt Champion

    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-c

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

    I 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 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())

  • Lifetime Qt Champion

    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


Log in to reply