Results 1 to 6 of 6

Thread: QTableView and QSqlTableModel problem

  1. #1
    Join Date
    Jun 2008
    Posts
    33
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Unhappy QTableView and QSqlTableModel problem

    Hi.

    I want to know how to make sure that I wont can let some cell of my view empty.
    For example I doubleclicked cell and started write. I rite something and than I press enter or tab or arrow. But when I write nothing, when I let the cell empty and press enter or tab or narrow or click somewhere I want to application set the empty cell as current and call edit().
    It sounds pretty easy but I dont know which signal may i use. I tried to use dataChanged with top and bottom modelindexes but it doesnt work and I dont know why. Thisi im the code:
    Qt Code:
    1. void DB::dataChanged(const QModelIndex &top, const QModelIndex &bottom)
    2. {
    3. if(!top.isValid())
    4. return;
    5. QString data;
    6. data = top.data().toString();
    7. if (data.isEmpty())
    8. {
    9. view->setCurrentIndex(top);
    10. view->edit(top);
    11. }
    To copy to clipboard, switch view to plain text mode 

    I tried some other signals from QTableView and QSqlTableModel but nothing works perfectly for me.

    Can anyone help me?

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableView and QSqlTableModel problem

    you can use your own delegate and then reimplement QItemDelegate::eventFilter.
    or more complex method, you can inherit from QTableView and reimplement QAbstractItemView::edit.

  3. #3
    Join Date
    Jun 2008
    Posts
    33
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableView and QSqlTableModel problem

    But why this dont work? Or is there other way to do that with signals-slots system?

    Can you tell me how to reimplement these functions? Please show me some example how to do that if you can.

    Thanks.

  4. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableView and QSqlTableModel problem

    Quote Originally Posted by Misenko View Post
    But why this dont work? Or is there other way to do that with signals-slots system?
    when you call setCurrentIndex then editor will be closed after that QItemDelegate::setModelData is called and empty value set in model.

    Qt Code:
    1. ...
    2. void LineEditDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
    3. const QModelIndex &index) const
    4. {
    5. QLineEdit *lineEdit = static_cast<QLineEdit*>(editor);
    6. QString value = lineEdit->text();
    7.  
    8. if (value.isEmpty()) {
    9. QMetaObject::invokeMethod(const_cast<LineEditDelegate *>(this), "keepEditorOpened", Qt::QueuedConnection, Q_ARG(QModelIndex, index));
    10. return;
    11. }
    12.  
    13. model->setData(index, value, Qt::EditRole);
    14. }
    15. ...
    To copy to clipboard, switch view to plain text mode 

    signal
    Qt Code:
    1. signals:
    2. void keepEditorOpened(const QModelIndex &index);
    To copy to clipboard, switch view to plain text mode 
    must be deslarated in lineeditdelegate.h

    then in widget do next
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
    2. {
    3. tableView = new QTableView();
    4. tableView->setModel(model);
    5.  
    6. LineEditDelegate *delegate = new LineEditDelegate();
    7. tableView->setItemDelegate(delegate);
    8. connect(delegate, SIGNAL(keepEditorOpened(const QModelIndex &)), SLOT(openEditor(const QModelIndex &)));
    9.  
    10. for (int row = 0; row < 4; ++row) {
    11. for (int column = 0; column < 2; ++column) {
    12. QModelIndex index = model->index(row, column, QModelIndex());
    13. model->setData(index, QVariant((row+1) * (column+1)));
    14. }
    15. }
    16. ...
    17. void MainWindow::openEditor(const QModelIndex &index)
    18. {
    19. tableView->edit(index);
    20. }
    To copy to clipboard, switch view to plain text mode 

    I hope this is not complicated example.

  5. #5
    Join Date
    Jun 2008
    Posts
    33
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableView and QSqlTableModel problem

    Thank you man it works perfectly with data which are already in database. But when I add new row it doesnt check it before I submit the model.
    Let me explain. I have model edit strategy set to QSqlTableModel::OnManualSubmit.
    So I have QPushButton apply on which I click this slot is activated:
    Qt Code:
    1. void DB::applyF()
    2. {
    3. if (!check())
    4. return;
    5. model->database().transaction();
    6. if (model->submitAll())
    7. {
    8. model->database().commit();
    9. }
    10. else
    11. {
    12. model->database().rollback();
    13. QMessageBox::warning(this, tr("Cached Table"),
    14. tr("The database reported an error: %1")
    15. .arg(model->lastError().text()));
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 

    and storno button whit this slot:
    Qt Code:
    1. void DB::stornoF()
    2. {
    3. model->revertAll();
    4. }
    To copy to clipboard, switch view to plain text mode 

    and add and remove buttons with thier slots:

    Qt Code:
    1. void DB::addF()
    2. {
    3. model->insertRow(model->rowCount());
    4. view->setCurrentIndex(model->index(model->rowCount()-1,0));
    5. view->edit(model->index(model->rowCount()-1,0));
    6. }
    7.  
    8. void DB::removeF()
    9. {
    10. model->removeRow(view->currentIndex().row());
    11. }
    To copy to clipboard, switch view to plain text mode 

    So when I press add row is added to the view only with asterisk and with remove row has exclamation mark until I press apply button.

    And with this added row before I press apply it doesnt work at all.

    How can I make it to check this cells too?

    PS: Sorry about my english

  6. #6
    Join Date
    Jun 2008
    Posts
    33
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableView and QSqlTableModel problem

    Sorry I was mistaken. It doesnt wark at all. When I let the cell empty end pres enter or tab or something it give back the text which was there but dont get it into edit mode. It explains the added rows it do the same with them but they were empty so given text is nothing. I think that the slot is not activated or signal is not send. How Can I fix it?
    Last edited by Misenko; 19th August 2008 at 23:57.

Similar Threads

  1. QTableView sorting problem
    By noktus in forum Newbie
    Replies: 11
    Last Post: 23rd April 2008, 10:20
  2. QSqlTableModel and QTableView and sorting
    By JeanC in forum Qt Programming
    Replies: 1
    Last Post: 5th April 2008, 13:22
  3. Problem with QTableView
    By BoneCollector in forum Qt Programming
    Replies: 5
    Last Post: 12th March 2008, 12:30
  4. QCompleter + QSqlTableModel problem
    By Lykurg in forum Qt Programming
    Replies: 2
    Last Post: 11th March 2007, 20:59

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.