Results 1 to 4 of 4

Thread: Access an object from a promoted class: How can i do that?

  1. #1
    Join Date
    Mar 2015
    Posts
    105
    Thanks
    50

    Default Access an object from a promoted class: How can i do that?

    Hello!
    I have this "TableHist" class where i implement a mousePressEvent. I promoted a QTableWidget to "TableHist". When i click on this table with right, left and middle button, that table should do different things. I say "should do" because i want to access an object created in another class first, but i don't know how to get access. How can i do that?


    Qt Code:
    1. #ifndef TABLEHIST_H
    2. #define TABLEHIST_H
    3.  
    4. #include <iostream>
    5. #include <QMouseEvent>
    6. #include <QTableWidget>
    7.  
    8. // Object i want to access.
    9. #include "database.h"
    10.  
    11. using namespace std;
    12.  
    13. class TableHist : public QTableWidget {
    14. public:
    15. TableHist(QWidget *parent = 0);
    16. virtual void mousePressEvent(QMouseEvent *event);
    17. };
    18.  
    19. #endif // TABLEHIST_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "tablehist.h"
    2.  
    3. TableHist::TableHist(QWidget* parent ) : QTableWidget( parent ) {}
    4.  
    5. void TableHist::mousePressEvent(QMouseEvent *event) {
    6.  
    7. QTableWidget::mousePressEvent( event );
    8. if(event->button( ) == Qt::RightButton) {
    9. //Do something with that object.
    10. }
    11. else if (event->button( ) == Qt::LeftButton) {
    12. //Do something with that object.
    13. }
    14. else {
    15. //Do something with that object.
    16. }
    17.  
    18. }
    To copy to clipboard, switch view to plain text mode 

    Thanks!

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Access an object from a promoted class: How can i do that?

    I am not sure I fully understand what you mean.

    Simply pass the other object to the TableHist object with a setter method?

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    robgeek (8th December 2015)

  4. #3
    Join Date
    Mar 2015
    Posts
    105
    Thanks
    50

    Default Re: Access an object from a promoted class: How can i do that?

    But since this class is used for mousepressevent i don't know where the object of TableHist is instantiated so i can use some "db.setDataBase(...)" method, like you said. That is the real problem, i don't know where this object is instantiated so i can use it. I believe he is instantiated in somewhere because the QTableWidget uses my mousepressevent.
    Last edited by robgeek; 8th December 2015 at 21:48.

  5. #4
    Join Date
    Mar 2015
    Posts
    105
    Thanks
    50

    Default Re: Access an object from a promoted class: How can i do that?

    Problem solved guys. I realized that the instantied object is the table i see when i start my program. So, i created a setter method just like anda_skoa said.

    But now i'm having another problem. I have this mousepressevent and i want to test it. I want to see (row, collumn) when i press the left button. But when i do that, nothing happens, "cout" just shows me (x, y) when i press other button after that. It seems he holds the result(x, y) and shows me when i click in another button.

    Qt Code:
    1. void TableHist::mousePressEvent(QMouseEvent *event) {
    2.  
    3. QTableWidget::mousePressEvent( event );
    4. if(event->button( ) == Qt::RightButton) {
    5. cout << "Right" << endl;
    6. }
    7. else if (event->button( ) == Qt::LeftButton) {
    8. cout << "(" << this->currentRow( ) << ", " << this->currentColumn( ) << "): ";
    9. }
    10. else {
    11. cout << "Middle" << endl;
    12. }
    13.  
    14. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Access caller qml-Object from C++ Object
    By xdn in forum Qt Quick
    Replies: 5
    Last Post: 14th February 2013, 09:58
  2. Replies: 2
    Last Post: 18th June 2012, 11:58
  3. show object (scene) in widget in class class mainwindow
    By rimie23 in forum Qt Programming
    Replies: 8
    Last Post: 1st May 2012, 16:15
  4. Replies: 11
    Last Post: 14th March 2010, 22:00
  5. Replies: 4
    Last Post: 26th June 2007, 19:19

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.