Results 1 to 4 of 4

Thread: QTableView, Delegate and multi font

  1. #1
    Join Date
    Jan 2015
    Posts
    35
    Thanks
    20
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default QTableView, Delegate and multi font

    Hi,

    I want to use multi font in QTableview so created my newclass (QSqlRelationalTableModel
    with virtual QVariant data(const QModelIndex & index, int role = Qt:isplayRole) const)

    Now each row can change the font I want. but when I select and change data with ItemDelegate (Dropdown List) and click next cell. new selected data is only shown id (key). (please see picture Style col's row 1 only show 4.
    But I submit, refresh and data is save and table is correct.

    1. How can I fix it?
    2. I want to change delegate list's font according to cell's font. How can I do it?

    Thanks.

    Untitled.jpg

    my code:

    Qt Code:
    1. #ifndef MYMODEL_H
    2. #define MYMODEL_H
    3.  
    4. #include <QObject>
    5. #include <QtSql/QSqlRelationalTableModel>
    6.  
    7. class myModel : public QSqlRelationalTableModel
    8. {
    9. Q_OBJECT
    10. public:
    11. explicit myModel(QObject *parent = 0, QSqlDatabase db = QSqlDatabase());
    12. ~myModel();
    13.  
    14. virtual QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;
    15.  
    16.  
    17. signals:
    18.  
    19. public slots:
    20.  
    21. private:
    22.  
    23. };
    24.  
    25. #endif // MYMODEL_H
    To copy to clipboard, switch view to plain text mode 



    Qt Code:
    1. #include "mymodel.h"
    2. #include <QFont>
    3. #include <QDebug>
    4. #include <QBrush>
    5.  
    6.  
    7. myModel::myModel(QObject *parent, QSqlDatabase db)
    8. {
    9. // qDebug() << db.isOpen() << " is open";
    10. }
    11.  
    12. myModel::~myModel()
    13. {
    14.  
    15. }
    16.  
    17. QVariant myModel::data(const QModelIndex &index, int role) const
    18. {
    19. switch( role )
    20. {
    21. case Qt::FontRole:
    22. if ( index.column()==1 ) return QVariant::fromValue(QFont("Aharoni", 12, QFont::Normal));
    23. else return QVariant::fromValue(QFont("Browallia New", 12, QFont::Normal));
    24. break;
    25.  
    26. case Qt::ForegroundRole:
    27. if ((index.row()%2)==1) return QBrush( Qt::red );
    28. else return QBrush( Qt::blue );
    29. break;
    30.  
    31. default:
    32. return QSqlTableModel::data(index,role);
    33. }
    34.  
    35. }
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. #include <QFile>
    2. #include <QDebug>
    3. #include <QSqlRelationalDelegate>
    4.  
    5. #include <QSql>
    6. #include <QSqlQuery>
    7.  
    8.  
    9. #include "mymodel.h"
    10.  
    11. namespace Ui {
    12. class myEditor;
    13. }
    14.  
    15. class myEditor : public QDialog
    16. {
    17. Q_OBJECT
    18.  
    19. public:
    20. explicit myEditor(QWidget *parent = 0);
    21. ~myEditor();
    22.  
    23. private slots:
    24. void on_pushButton_clicked();
    25.  
    26. private:
    27. Ui::myEditor *ui;
    28.  
    29. QSqlQuery *qStyle;
    30. myModel *model;
    31.  
    32. void openDB();
    33.  
    34. };
    35.  
    36. #endif // MYEDITOR_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "myeditor.h"
    2. #include "ui_myeditor.h"
    3.  
    4. myEditor::myEditor(QWidget *parent) :
    5. QDialog(parent),
    6. ui(new Ui::myEditor)
    7. {
    8. ui->setupUi(this);
    9.  
    10. openDB();
    11. if(!myDb.open()) { qDebug() << "database opening error song"; return; }
    12.  
    13. model = new myModel(this,myDb);
    14.  
    15. model->setTable("MainData");
    16. model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    17.  
    18. model->setRelation(1, QSqlRelation("myStyle", "id", "stylename"));
    19. model->select();
    20.  
    21. model->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
    22. model->setHeaderData(1, Qt::Horizontal, QObject::tr("Style"));
    23. model->setHeaderData(2, Qt::Horizontal, QObject::tr("Description"));
    24.  
    25. ui->tableView->setModel(model);
    26. ui->tableView->setItemDelegate(new QSqlRelationalDelegate(ui->tableView));
    27.  
    28. }
    29.  
    30. myEditor::~myEditor()
    31. {
    32. delete ui;
    33. }
    34.  
    35. void myEditor::openDB()
    36. {
    37. QString strdbPath= QDir::currentPath() + "/mydata.db3";
    38. QFile fn(strdbPath);
    39. if(!fn.exists())
    40. qDebug() << strdbPath << " is missing!" << endl;
    41. else
    42. qDebug() << "found at " << strdbPath;
    43.  
    44. myDb = QSqlDatabase::addDatabase("QSQLITE","mycon");
    45. myDb.setDatabaseName(strdbPath);
    46.  
    47. }
    48.  
    49. void myEditor::on_pushButton_clicked()
    50. {
    51. model->submitAll();
    52. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QTableView, Delegate and multi font

    Your data() override returns the QSqlTableModel::data() value for values not overridden but the class is derived from QSqlRelationalTableModel. Return the base class data() value.

  3. #3
    Join Date
    Jan 2015
    Posts
    35
    Thanks
    20
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: QTableView, Delegate and multi font

    Thanks for your suggestion.


    Now I changed QSqlTableModel to QSqlRelationalTableModel. It's ok.

    I want to also change Delegate List's Font according to selected cell's font style.
    How can I set delegate List's font?

    Thanks.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QTableView, Delegate and multi font

    Provide your own delegate and set the font on the QComboBox you create as the editor based on the cell it has been opened on.

Similar Threads

  1. Custom Delegate in multi hierarchy QTreeView
    By btbt9527 in forum Qt Programming
    Replies: 0
    Last Post: 5th January 2016, 18:28
  2. How to do QChartView delegate like QTableView item delegate
    By malleeswarareddy.s in forum Qt Programming
    Replies: 0
    Last Post: 16th September 2015, 11:37
  3. QTableView doesn't use delegate
    By treaves in forum Qt Programming
    Replies: 3
    Last Post: 1st February 2010, 09:45
  4. QTableView Delegate Usage
    By rajeshs in forum Qt Programming
    Replies: 1
    Last Post: 14th April 2008, 07:03
  5. Multi-line messages in QTableView
    By Conel in forum Qt Programming
    Replies: 6
    Last Post: 13th April 2006, 13:49

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.