PDA

View Full Version : Access an object from a promoted class: How can i do that?



robgeek
8th December 2015, 17:29
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?



#ifndef TABLEHIST_H
#define TABLEHIST_H

#include <iostream>
#include <QMouseEvent>
#include <QTableWidget>

// Object i want to access.
#include "database.h"

using namespace std;

class TableHist : public QTableWidget {
public:
TableHist(QWidget *parent = 0);
virtual void mousePressEvent(QMouseEvent *event);
};

#endif // TABLEHIST_H


#include "tablehist.h"

TableHist::TableHist(QWidget* parent ) : QTableWidget( parent ) {}

void TableHist::mousePressEvent(QMouseEvent *event) {

QTableWidget::mousePressEvent( event );
if(event->button( ) == Qt::RightButton) {
//Do something with that object.
}
else if (event->button( ) == Qt::LeftButton) {
//Do something with that object.
}
else {
//Do something with that object.
}

}

Thanks!

anda_skoa
8th December 2015, 19:23
I am not sure I fully understand what you mean.

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

Cheers,
_

robgeek
8th December 2015, 21:48
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.

robgeek
8th December 2015, 22:20
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.


void TableHist::mousePressEvent(QMouseEvent *event) {

QTableWidget::mousePressEvent( event );
if(event->button( ) == Qt::RightButton) {
cout << "Right" << endl;
}
else if (event->button( ) == Qt::LeftButton) {
cout << "(" << this->currentRow( ) << ", " << this->currentColumn( ) << "): ";
}
else {
cout << "Middle" << endl;
}

}