Originally Posted by
high_flyer
I don't understand why signal and slots are a problem.
I use multiple inheritance with Qt, many times in order to give external non Qt classes the ability to use signal and slots and I have no problem.
Could you give an example for a class and a compiler erros it gives you?
You have to make sure you have Q_OBJECT macro in your class decleration.
Ok, first we define a base class:
class Document
: virtual public QWidget { Q_OBJECT
public:
virtual ~Document() {}
public slots:
virtual void method
() {QMessageBox::warning(0,
"bla",
"doc");
} };
class Document : virtual public QWidget {
Q_OBJECT
public:
Document(QWidget *p, const char *c) : QWidget(p, c) { }
virtual ~Document() {}
public slots:
virtual void method() {QMessageBox::warning(0, "bla", "doc");}
};
To copy to clipboard, switch view to plain text mode
Then we define a derived class:
class Table : virtual public QTable, virtual public Document {
public:
Table
(QWidget *p,
const char *n
) : QTable
(p, n
), Document
(NULL,
NULL) { }
virtual ~Table() {
}
virtual void method
() { QMessageBox::warning(0,
"bla",
"table");
} };
class Table : virtual public QTable, virtual public Document {
public:
Table(QWidget *p, const char *n) : QTable(p, n), Document(NULL, NULL) {
}
virtual ~Table() {
}
virtual void method() { QMessageBox::warning(0, "bla", "table"); }
};
To copy to clipboard, switch view to plain text mode
Then we make some test code:
Document *doc = new Table(this, "");
action
= new QAction( tr
("Test"), tr
("&Test"), CTRL
+Key_T,
this );
connect( action, SIGNAL( activated() ), doc, SLOT( method() ) );
Document *doc = new Table(this, "");
action = new QAction( tr("Test"), tr("&Test"), CTRL+Key_T, this );
connect( action, SIGNAL( activated() ), doc, SLOT( method() ) );
To copy to clipboard, switch view to plain text mode
This works fine. But what if you want to add slots and signals to the Table class? Well, we add the Q_OBJECT macro, and add a public slot thingy with a specific method, like so:
class Table : virtual public QTable, virtual public Document {
Q_OBJECT
public:
Table
(QWidget *p,
const char *n
) : QTable
(p, n
), Document
(NULL,
NULL) { }
virtual ~Table() {
}
virtual void method
() { QMessageBox::warning(0,
"bla",
"table");
} public slots:
void specific_slot
() { QMessageBox::warning(0,
"bla",
"slotty");
} };
class Table : virtual public QTable, virtual public Document {
Q_OBJECT
public:
Table(QWidget *p, const char *n) : QTable(p, n), Document(NULL, NULL) {
}
virtual ~Table() {
}
virtual void method() { QMessageBox::warning(0, "bla", "table"); }
public slots:
void specific_slot() { QMessageBox::warning(0, "bla", "slotty"); }
};
To copy to clipboard, switch view to plain text mode
However, when compiling this, Visual Studio throws a hissy fit, saying
c:\projects\mi\table.h(7) : error C2594: 'type cast' : ambiguous conversions from 'class Table *const ' to 'class QObject *'
This is the very line in the table.h file, which contains the Q_OBJECT macro.
It can't be good design if you have to include all the slots from derived classes in the base class. :|
Bookmarks