Results 1 to 11 of 11

Thread: Connect external widget(slider, linedit) to QTableWidgetItem

  1. #1
    Join Date
    Nov 2010
    Posts
    142
    Thanks
    24
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Connect external widget(slider, linedit) to QTableWidgetItem

    I am trying to connect external widgets(slider, combobox, lineedit, etc) to specific QTableWidgetItem.
    The widgets are not embed into the QTableWidget.
    All the widgets have their own Signals, which is really fantastic,
    BUT the QTableWidgetItem have no SLOTS!
    How can i set the values of the QTableWidgetItems using other widgets outside the QTableWidget?
    I tried something like the code below but it simply does not work

    Qt Code:
    1. //setting up the table
    2. QTableWidget *dataSheet = new QTableWidget;
    3. dataSheet->setRowCount(3);
    4. dataSheet->setColumnCount(7);
    5.  
    6. //initializing the QTableWidgetItem
    7. int row = 0, col = 0;
    8. QTableWidgetItem *newItem = new QTableWidgetItem(tr("%1").arg(343.3432));
    9. dataSheet->setItem(row, col, newItem);
    10.  
    11. //creating external widget
    12. QLineEdit *action = new QLineEdit;
    13.  
    14. //trying to connect?!
    15. connect(action, SIGNAL( textChanged ( const QString & ) ), newItem, SLOT( setText ( const QString & ) ));
    To copy to clipboard, switch view to plain text mode 

    any suggestions?!

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Connect external widget(slider, linedit) to QTableWidgetItem

    BUT the QTableWidgetItem have no SLOTS!
    That's because QTableWidgetItem is not a QObject.

    How can i set the values of the QTableWidgetItems using other widgets outside the QTableWidget?
    You can make a slot in your table widget, connect to that slot, and in it set the data to the items you want.

    But it would better if you understand how Model/View works, to deal with data correctly through the model.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Nov 2010
    Posts
    142
    Thanks
    24
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Connect external widget(slider, linedit) to QTableWidgetItem

    You can make a slot in your table widget, connect to that slot, and in it set the data to the items you want.
    QTableWidget and the other widgets(slider, lineedit, etc) belong to one-central widget MyWidget.
    do you mean to overload the QTableWidget class inserting a custom SLOT or to just add a SLOT to MyWidget?

  4. #4
    Join Date
    Nov 2010
    Posts
    142
    Thanks
    24
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Connect external widget(slider, linedit) to QTableWidgetItem

    i created this (doesn't work)


    Qt Code:
    1. class MyTableItem : public QTableWidgetItem
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6.  
    7. MyTableItem(const QString &);
    8.  
    9.  
    10. public Q_SLOTS:
    11. void setAction(const QString &);
    12.  
    13.  
    14. };
    To copy to clipboard, switch view to plain text mode 

    when I remove the Q_OBJECT I cannot add a public Q_SLOTS
    When I add the Q_OBJECT I get several errors like

    Qt Code:
    1. error: ‘staticMetaObject’ is not a member of ‘QTableWidgetItem’
    2. /usr/include/qt4/QtCore/qobject.h:296: error: ‘QScopedPointer<QObjectData, QScopedPointerDeleter<QObjectData> QObject::d_ptr’ is protected
    To copy to clipboard, switch view to plain text mode 

    The idea is mainly to overload the QTableWidgetItem in order to add slots.
    Am i completely wrong?

  5. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Connect external widget(slider, linedit) to QTableWidgetItem

    when I remove the Q_OBJECT I cannot add a public Q_SLOTS
    When I add the Q_OBJECT I get several errors like
    Again, QTableWidgetItem is NOT a QObject.
    Only QObject can use the Q_OBJECT macro, and use signals/ slots.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  6. #6
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Connect external widget(slider, linedit) to QTableWidgetItem

    As high_flyer said, QTableWidgetItem is not a QObject, if your class inherits only QTreeWidgetItem it wont be a QObject either.
    You need to subclass QObject as well:
    Qt Code:
    1. class MyTableItem : public QObject, public QTableWidgetItem
    To copy to clipboard, switch view to plain text mode 

  7. The following user says thank you to stampede for this useful post:

    fatecasino (2nd February 2011)

  8. #7
    Join Date
    Nov 2010
    Posts
    142
    Thanks
    24
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Connect external widget(slider, linedit) to QTableWidgetItem

    thank you very much! It worked indeed:

    MyTableItem.h
    Qt Code:
    1. class MyTableItem : public QObject, public QTableWidgetItem
    2. {
    3.  
    4. Q_OBJECT
    5.  
    6. public:
    7.  
    8. MyTableItem(const QString &);
    9.  
    10.  
    11. public slots:
    12. void setTextSlot(const QString &);
    13.  
    14.  
    15. };
    To copy to clipboard, switch view to plain text mode 

    myTableItem.cpp
    Qt Code:
    1. #include <myTableItem.h>
    2.  
    3. MyTableItem ::MyTableItem (const QString & text)
    4. {
    5. setText(text);
    6. }
    7.  
    8.  
    9.  
    10. void MyTableItem ::setTextSlot(const QString & t)
    11. {
    12. setText(t);
    13.  
    14. }
    To copy to clipboard, switch view to plain text mode 

    in the main
    Qt Code:
    1. MyTable *dataSheet = new MyTable;//i have overloaded the QTableWidget too
    2. MyTableItem *test = new MyTableItem("test");
    3. dataSheet->setItem(2, 2, test);
    4. QLineEdit *action = new QLineEdit;
    5. connect(action, SIGNAL( textChanged ( const QString & ) ), test, SLOT( setTextSlot ( const QString & ) ));
    To copy to clipboard, switch view to plain text mode 

  9. #8
    Join Date
    Nov 2010
    Posts
    142
    Thanks
    24
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Connect external widget(slider, linedit) to QTableWidgetItem

    Now, I have a slightly different problem...
    I want the action QLineEdit to set the values of a specific cell of the MyTable dataSheet (it's an overloaded QTableWidget):


    Qt Code:
    1. MyTable *dataSheet = new MyTable;//i have overloaded the QTableWidget too
    2. MyTableItem *test = new MyTableItem("test");
    3. dataSheet->setItem(2, 2, test);
    4. QLineEdit *action = new QLineEdit;
    5. connect(action, SIGNAL( textChanged ( const QString & ) ),dataSheet->item(2,2) , SLOT( setTextSlot ( const QString & ) ));
    To copy to clipboard, switch view to plain text mode 

    I get the following error:

    Qt Code:
    1. error: no matching function for call to ‘MyWidget::connect(MyLineEdit*&, const char*, QTableWidgetItem*, const char*)’
    2. /usr/include/qt4/QtCore/qobject.h:198: candidates are: static bool QObject::connect(const QObject*, const char*, const QObject*, const char*, Qt::ConnectionType)
    3. /usr/include/qt4/QtCore/qobject.h:313: bool QObject::connect(const QObject*, const char*, const char*, Qt::ConnectionType) const
    To copy to clipboard, switch view to plain text mode 

    I think I am close to make it work, but there's something wrong.

    From the moment that this line works:

    Qt Code:
    1. connect(action, SIGNAL( textChanged ( const QString & ) ), test, SLOT( setTextSlot ( const QString & ) ));
    To copy to clipboard, switch view to plain text mode 

    there must be a way to make this line work too:

    Qt Code:
    1. connect(action, SIGNAL( textChanged ( const QString & ) ),dataSheet->item(2,2) , SLOT( setTextSlot ( const QString & ) ));
    To copy to clipboard, switch view to plain text mode 

  10. #9
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Connect external widget(slider, linedit) to QTableWidgetItem

    Probably you haven't implemented the "item" method in your QTreeWidget subclass to return your custom items, so "item" still returns QTreeWidgetItem*, which is not a QObject and thus cannot be used in signal / slot mechanism.
    If you want to go this way, one of solutions is - implement ' MyTable::item(int,int) ' to return ' MyTableItem* '.

  11. #10
    Join Date
    Nov 2010
    Posts
    142
    Thanks
    24
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Connect external widget(slider, linedit) to QTableWidgetItem

    Did you mean this?

    Qt Code:
    1. MyTableItem* MyTable::item ( int row, int column ) const
    2. {
    3. return item ( row, column );
    4. }
    To copy to clipboard, switch view to plain text mode 

    It compiles fine, but I get segmentation fault because of this

    the source code has something like this:
    Qt Code:
    1. return tableItems.value(tableIndex(row, column));
    To copy to clipboard, switch view to plain text mode 

    but i don't know how to transform it to my class

  12. #11
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Connect external widget(slider, linedit) to QTableWidgetItem

    segmentation fault because of this
    Yes, this is correct behaviour according to the implementation

    Try to imagine what happens when you call
    Qt Code:
    1. item(2,2);
    To copy to clipboard, switch view to plain text mode 
    ...
    ( small hint: item(2,2) => { return item(2,2) => { return item(2,2) => ... } } )
    Calling the same method with the same arguments over and over...

    What you want to do is to call base class implementation of "item" from your class "item" method and convert the result to your datatype. So there are two more questions:
    1) how to call base class implementation from derived class method ?
    2) how to convert from base class pointer to derived class pointer at runtime ?

    Can't help you more with this, because next step is to write ~30 more characters in your "item" implementation Believe me, you'll learn much more by doing this yourself ( there is great, free C++ book available online called "Thinking in C++" )

Similar Threads

  1. Replies: 22
    Last Post: 14th October 2010, 18:44
  2. sync plot grid w/ external scale widget
    By Kevin Ching in forum Qwt
    Replies: 6
    Last Post: 30th September 2010, 18:12
  3. Replies: 2
    Last Post: 21st March 2010, 10:01
  4. Dock widget does not have a size slider
    By rakkar in forum Newbie
    Replies: 3
    Last Post: 9th September 2009, 09:33
  5. Replies: 1
    Last Post: 14th June 2006, 15:36

Tags for this Thread

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.