Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] cellChanged doesn't work with QTimeEdit?

[SOLVED] cellChanged doesn't work with QTimeEdit?

Scheduled Pinned Locked Moved General and Desktop
16 Posts 2 Posters 5.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Leon
    wrote on last edited by
    #5

    [quote author="SGaist" date="1380611472"]I see two possibilities:

    • Use the timeChanged signal from your QTimeEdit
    • Create a QStyleItemDelegate that uses a QTimeEdit as editor[/quote]

    I went with the first one since i am not experienced with the StyleItemDelegate

    i did someting like
    @QTimeEdit *end_time=new QTimeEdit(this);
    connect(end_time, SIGNAL(timeChanged(QTime)), this, SLOT(time_has_changed(QTime)));
    ui->tableWidget->setCellWidget(last_row, 3, end_time);@

    it works
    but i want to know in which row the qtimeedit changed... using currentitem,current row etc always returns 0x0 ...

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #6

      The only way I see would be to keep a map of your QTimeEdit and their position.
      You really should implement an item delegate, there's an example for the double spin box that you can use as a base.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • L Offline
        L Offline
        Leon
        wrote on last edited by
        #7

        [quote author="SGaist" date="1380632857"]The only way I see would be to keep a map of your QTimeEdit and their position.
        You really should implement an item delegate, there's an example for the double spin box that you can use as a base.[/quote]

        You mean the spinboxdelegate example? Ok i will take a look at it and will post my results tommorow ;)

        1 Reply Last reply
        0
        • L Offline
          L Offline
          Leon
          wrote on last edited by
          #8

          I am confused with the "spinboxdelegate example":http://qt-project.org/doc/qt-4.8/itemviews-spinboxdelegate.html

          Mostly with the line
          @SpinBoxDelegate delegate;
          tableView.setItemDelegate(&delegate);@

          So what i am trying to do is to make the same exact example BUT, with a ui that i have added a tableview via the designer AND without any code at main.cpp.. So my mainwindow.cpp:

          @#include "mainwindow.h"
          #include "ui_mainwindow.h"

          #include <QStandardItemModel>
          #include <QSpinBox>

          MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
          {
          ui->setupUi(this);

          QStandardItemModel model(4, 2);
          ui->tableView->setModel(&model);
          
          //MainWindow delegate;
          //ui->tableView->setItemDelegate(&delegate);
          
          ui->tableView->horizontalHeader()->setStretchLastSection(true);
          
          for (int row = 0; row < 4; ++row) {
              for (int column = 0; column < 2; ++column) {
                  QModelIndex index = model.index(row, column, QModelIndex());
                  model.setData(index, QVariant((row + 1) * (column + 1)));
              }
          }
          
          this->setWindowTitle(QObject::tr("Spin Box Delegate"));
          

          }

          MainWindow::~MainWindow()
          {
          delete ui;
          }

          QWidget *MainWindow::createEditor(QWidget parent,
          const QStyleOptionViewItem &/
          option /,
          const QModelIndex &/
          index */) const
          {
          QSpinBox *editor = new QSpinBox(parent);
          editor->setFrame(false);
          editor->setMinimum(0);
          editor->setMaximum(100);

          return editor;
          

          }

          void MainWindow::setEditorData(QWidget *editor,
          const QModelIndex &index) const
          {
          int value = index.model()->data(index, Qt::EditRole).toInt();

          QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
          spinBox->setValue(value);
          

          }

          void MainWindow::setModelData(QWidget *editor, QAbstractItemModel *model,
          const QModelIndex &index) const
          {
          QSpinBox spinBox = static_cast<QSpinBox>(editor);
          spinBox->interpretText();
          int value = spinBox->value();

          model->setData(index, value, Qt::EditRole);
          

          }

          void MainWindow::updateEditorGeometry(QWidget editor,
          const QStyleOptionViewItem &option, const QModelIndex &/
          index */) const
          {
          editor->setGeometry(option.rect);
          }
          @

          The result is an empty tableview simple because of
          @ //MainWindow delegate;
          //ui->tableView->setItemDelegate(&delegate);@

          If uncomment these 2 lines they produce errors...

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #9

            You're not using it properly, it works in the example because the delegate scope is the main function. In your case, it's destroyed at the end of the constructor (and so should be your model).

            Why do you have a MainWindow as delegate in your MainWindow that is a QMainWindow ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • L Offline
              L Offline
              Leon
              wrote on last edited by
              #10

              You are right about the model, i put it out of the constructor and it worked ;)

              [quote author="SGaist" date="1380728064"]
              Why do you have a MainWindow as delegate in your MainWindow that is a QMainWindow ?[/quote]

              Yeah these lines was the ones that confused me a lot...

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #11

                Then rename your delegate to something less confusing :) TimeEditDelegate for example :)

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • L Offline
                  L Offline
                  Leon
                  wrote on last edited by
                  #12

                  [quote author="SGaist" date="1380747885"]Then rename your delegate to something less confusing :) TimeEditDelegate for example :)[/quote]

                  What do you mean rename the delegate?

                  @MainWindow delegate;
                  ui->tableView->setItemDelegate(&delegate);@

                  always produces an error of
                  @C2664: 'QAbstractItemView::setItemDelegate' : cannot convert parameter 1 from 'MainWindow **' to 'QAbstractItemDelegate *'
                  Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast@

                  How i am supposed to change this line?
                  @MainWindow::MainWindow(QWidget *parent) :
                  QMainWindow(parent),
                  ui(new Ui::MainWindow)
                  {@

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #13

                    You should have a separate class the is derived from QStyleItemDelegate, not just add the methods to your MainWindow class.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • L Offline
                      L Offline
                      Leon
                      wrote on last edited by
                      #14

                      Ok i did..

                      "result?":http://i.imgur.com/3gmpFUg.png

                      "while we wanted something like":http://i.imgur.com/Xb0FiSJ.png

                      mainwindow.h

                      @#ifndef MAINWINDOW_H
                      #define MAINWINDOW_H

                      #include <QMainWindow>
                      #include "QTableWidgetItem"
                      #include <QStyledItemDelegate>

                      namespace Ui {
                      class MainWindow;
                      }

                      class MainWindow : public QMainWindow
                      {
                      Q_OBJECT

                      public:
                      explicit MainWindow(QWidget *parent = 0);
                      ~MainWindow();

                      private:
                      Ui::MainWindow *ui;
                      };

                      class TableViewDelegate : public QStyledItemDelegate
                      {
                      Q_OBJECT

                      public:
                      TableViewDelegate(QObject *parent = 0);

                      QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
                                            const QModelIndex &index) const;
                      
                      void setEditorData(QWidget *editor, const QModelIndex &index) const;
                      void setModelData(QWidget *editor, QAbstractItemModel *model,
                                        const QModelIndex &index) const;
                      
                      void updateEditorGeometry(QWidget *editor,
                          const QStyleOptionViewItem &option, const QModelIndex &index) const;
                      

                      };

                      #endif // MAINWINDOW_H@

                      mainwindow.cpp

                      @#include "mainwindow.h"
                      #include "ui_mainwindow.h"

                      #include <QTimeEdit>
                      #include <QDebug>
                      #include <QStandardItemModel>

                      QStandardItemModel model(4, 2);

                      MainWindow::MainWindow(QWidget *parent) :
                      QMainWindow(parent),
                      ui(new Ui::MainWindow)
                      {
                      ui->setupUi(this);

                      ui->tableView->setModel(&model);
                      
                      TableViewDelegate delegate;
                      ui->tableView->setItemDelegate(&delegate);
                      
                      ui->tableView->horizontalHeader()->setStretchLastSection(true);
                      

                      }

                      MainWindow::~MainWindow()
                      {
                      delete ui;
                      }

                      TableViewDelegate::TableViewDelegate(QObject *parent)
                      : QStyledItemDelegate(parent)
                      {
                      for (int row = 0; row < 4; ++row) {
                      for (int column = 0; column < 2; ++column) {
                      QModelIndex index = model.index(row, column, QModelIndex());
                      model.setData(index, QTime(12,04));
                      }
                      }
                      }

                      QWidget *TableViewDelegate::createEditor(QWidget parent,
                      const QStyleOptionViewItem &/
                      option /,
                      const QModelIndex &/
                      index */) const
                      {
                      QTimeEdit *editor = new QTimeEdit(parent);
                      editor->setFrame(false);
                      //editor->setMinimum(0);
                      //editor->setMaximum(100);

                      return editor;
                      

                      }

                      void TableViewDelegate::setEditorData(QWidget *editor,
                      const QModelIndex &index) const
                      {
                      QString value = index.model()->data(index, Qt::EditRole).toString();

                      QTimeEdit *timeedit = static_cast<QTimeEdit*>(editor);
                      timeedit->setTime(QTime::fromString(value));
                      

                      }

                      void TableViewDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
                      const QModelIndex &index) const
                      {
                      QTimeEdit timeedit = static_cast<QTimeEdit>(editor);
                      timeedit->interpretText();
                      QString value = timeedit->time().toString();

                      model->setData(index, value, Qt::EditRole);
                      

                      }

                      void TableViewDelegate::updateEditorGeometry(QWidget editor,
                      const QStyleOptionViewItem &option, const QModelIndex &/
                      index */) const
                      {
                      editor->setGeometry(option.rect);
                      }
                      @

                      i guess it has someting to do that TableViewDelegate doesn't have access to the ui, thus doesnt have access to the tableview.. What can i do for this?

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #15

                        Your delegate is still destroyed at the end of the constructor

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        0
                        • L Offline
                          L Offline
                          Leon
                          wrote on last edited by
                          #16

                          Yep you are right.
                          Came back to mark as solved and say thanks ;)

                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved