Results 1 to 3 of 3

Thread: How to Save (Update) and Change Font for each Cell in QTableView

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

    Default How to Save (Update) and Change Font for each Cell in QTableView

    Hi,

    I created Qt C++ simple sqlite database program with QSqlRelationalTableModel.
    Open database and view with QSqlRelationalTableModel is ok.

    Now I edit data in QTableView (in my program tableView on Form) then I want to save new update data on database.
    Next When I change or select style on tableView I want to change the specific font of this row's next cell (Description)

    Question 1 is how can I save new update data to database?
    Question 2 is how can I change each cell with different font?

    Thanks.

    My code:

    mainwindow.h

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5.  
    6. #include <QSql>
    7. #include <QSqlRelationalTableModel>
    8.  
    9. namespace Ui {
    10. class MainWindow;
    11. }
    12.  
    13. class MainWindow : public QMainWindow
    14. {
    15. Q_OBJECT
    16.  
    17. public:
    18. explicit MainWindow(QWidget *parent = 0);
    19. ~MainWindow();
    20.  
    21. private:
    22. Ui::MainWindow *ui;
    23.  
    24. void openDB();
    25.  
    26. };
    27.  
    28. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 


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


    my form UI
    ui.jpgui.jpg


    database

    Qt Code:
    1. mydata.db3
    2.  
    3. ====================================
    4. 1. (table) MainData
    5. ------------------------------------
    6. id styleid Description
    7. ------------------------------------
    8. 100 1 Sample Main Style
    9. 101 2 Traditional Theme
    10. 102 3 Musical Symbol
    11. 203 4 Minority Group
    12. 204 5 Hill with Cloud
    13. 205 4 Thai
    14. 206 2 Ancient Theme
    15. 207 3 Air Symbol
    16. 208 1 Principle Theme
    17. 209 5 Middle One
    18.  
    19. =====================================
    20. 2. (table) myStyle
    21. -------------------------------------
    22. id styleName
    23. -------------------------------------
    24. 1 Main Style
    25. 2 Traditional Style
    26. 3 Musical Style
    27. 4 Ethnic Group Style
    28. 5 Regional Style
    To copy to clipboard, switch view to plain text mode 
    Last edited by anda_skoa; 25th March 2016 at 10:19. Reason: changed [quote] to [code]

  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: How to Save (Update) and Change Font for each Cell in QTableView

    1. Call QSqlTableModel::submitAll().
    2. Either subclass QSqlRelationalTableModel and change data() to return a valid font for the Qt::FontRole, or put a QIdentityProxyModel in to achieve a similar override.

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

    Default Re: How to Save (Update) and Change Font for each Cell in QTableView

    Thanks.

    Update (Save) is done successfully.

    Changing Font for each cell is not test.

    I'll try it.
    Last edited by binary001; 25th March 2016 at 10:44.

Similar Threads

  1. Replies: 1
    Last Post: 31st October 2014, 21:45
  2. Replies: 7
    Last Post: 25th July 2013, 22:47
  3. Save Font settings using QSettings
    By arunkumaraymuo1 in forum Qt Programming
    Replies: 2
    Last Post: 31st July 2012, 14:58
  4. Change Font of QListWidget to Monospace Font
    By pospiech in forum Qt Programming
    Replies: 3
    Last Post: 25th July 2008, 19:23
  5. QTableView change color of current Cell
    By raphaelf in forum Qt Programming
    Replies: 4
    Last Post: 4th March 2006, 12:22

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.