I'm trying to subclass QQueue
in order to add some functionality.
Actually, I wasn't able to subclass but because the code is very short I rewrote my own implementation:
#ifndef MYQUEUE_H
#define MYQUEUE_H
#include <QObject>
#include <QtCore/qlist.h>
QT_BEGIN_NAMESPACE
template <class T>
class MyQueue : public QList<T>
{
Q_OBJECT
public:
// compiler-generated special member functions are fine!
inline void swap(MyQueue<T> &other) Q_DECL_NOTHROW { QList<T>::swap(other); } // prevent QList<->QQueue swaps
#ifndef Q_QDOC
// bring in QList::swap(int, int). We cannot say using QList<T>::swap,
// because we don't want to make swap(QList&) available.
inline void swap(int i, int j) { QList<T>::swap(i, j); }
#endif
inline void enqueue(const T &t) { QList<T>::append(t); emit enqueued(); }
inline T dequeue() { return QList<T>::takeFirst(); }
inline T &head() { return QList<T>::first(); }
inline const T &head() const { return QList<T>::first(); }
signals:
void enqueued();
};
QT_END_NAMESPACE
#endif // MYQUEUE_H
Basically, it emits a signal whenever something is enqueued. I don't know the correct syntax to bind that signal to a slot:
MyQueue<QString> queue;
connect(&queue, &MyQueue::enqueued, this, &MainWindow::process_queue);
error: 'template class MyQueue' used without template parameters connect(&queue, &MyQueue::enqueued, this, &MainWindow::process_queue); ^
It says that I'm using MyQueue
(that is a template class) without specifying the template parameters (QString
).
I tried to add it but I did in a wrong way:
connect(&queue, &MyQueue<QString>::enqueued, this, &MainWindow::process_queue);
error: no matching function for call to 'MainWindow::connect(MyQueue, void (MyQueue::)(), MainWindow*, void (MainWindow::*)())'
What's the right syntax to connect such a signal?
QObject
s, or its subclasses. – vahanchoMyQueue
inherit from a non-template base class and put the requiredsignal
in the base class? – G.M.