Let the example speak for itself:
// main.cpp
#include <QtGui>
{
Q_OBJECT
public:
{
connect(button, SIGNAL(clicked()), this, SLOT(paste()));
layout->addWidget(button);
for (int i = 0; i < 5; ++i)
{
lineEdit->installEventFilter(this); // <--
layout->addWidget(lineEdit);
}
}
{
if (event
->type
() == QEvent::FocusIn) {
focused = qobject_cast<QLineEdit*>(object); // <--
}
return false;
}
private slots:
void paste()
{
if (focused)
{
focused->setText(qApp->clipboard()->text());
}
}
private:
};
int main(int argc, char* argv[])
{
Window w;
w.show();
return a.exec();
}
#include "main.moc"
// main.cpp
#include <QtGui>
class Window : public QWidget
{
Q_OBJECT
public:
Window(QWidget* parent = 0) : QWidget(parent), focused(0)
{
QVBoxLayout* layout = new QVBoxLayout(this);
QPushButton* button = new QPushButton("Paste", this);
connect(button, SIGNAL(clicked()), this, SLOT(paste()));
layout->addWidget(button);
for (int i = 0; i < 5; ++i)
{
QLineEdit* lineEdit = new QLineEdit(this);
lineEdit->installEventFilter(this); // <--
layout->addWidget(lineEdit);
}
}
bool eventFilter(QObject* object, QEvent* event)
{
if (event->type() == QEvent::FocusIn)
{
focused = qobject_cast<QLineEdit*>(object); // <--
}
return false;
}
private slots:
void paste()
{
if (focused)
{
focused->setText(qApp->clipboard()->text());
}
}
private:
QLineEdit* focused;
};
int main(int argc, char* argv[])
{
QApplication a(argc, argv);
Window w;
w.show();
return a.exec();
}
#include "main.moc"
To copy to clipboard, switch view to plain text mode
Bookmarks