Home | ![]() |
This example shows an application that can swallow other windows. Obtain the X11 window ID of an arbitrary window with the utility "xwininfo", then paste this ID into the input field of the application and click "Embed".
#ifndef SWALLOW_H #define SWALLOW_H #include <qwidget.h> class QtXEmbedContainer; class QLineEdit; class QLabel; class Embedder : public QWidget { Q_OBJECT public: Embedder(QWidget *parent = 0, const char *name = 0, WFlags f = 0); private slots: void swallow(); void reject(); void showClientEmbedded(); void showClientClosed(); void showClientError(int err); private: QtXEmbedContainer *container; QLineEdit *input; QLabel *display; }; #endif
#include "swallow.h" #include <qlineedit.h> #include <qlayout.h> #include <qlabel.h> #include <qtextstream.h> #include <qpushbutton.h> #include <qtxembed.h> Embedder::Embedder(QWidget *parent, const char *name, WFlags f) : QWidget(parent, name, f) { QVBoxLayout *layout = new QVBoxLayout(this); QHBoxLayout *hbox = new QHBoxLayout(layout); input = new QLineEdit(this); hbox->addWidget(input); QPushButton *embedButton = new QPushButton("&Embed", this); hbox->addWidget(embedButton); container = new QtXEmbedContainer(this); layout->addWidget(container); QHBoxLayout *hbox2 = new QHBoxLayout(layout); QPushButton *button = new QPushButton("&Discard", this); hbox2->addWidget(button); QPushButton *quitButton = new QPushButton("&Quit", this); hbox2->addWidget(quitButton); display = new QLabel(this); display->setText("Ready."); layout->addWidget(display); connect(embedButton, SIGNAL(clicked()), SLOT(swallow())); connect(quitButton, SIGNAL(clicked()), SLOT(close())); connect(button, SIGNAL(clicked()), SLOT(reject())); connect(container, SIGNAL(clientIsEmbedded()), SLOT(showClientEmbedded())); connect(container, SIGNAL(clientClosed()), SLOT(showClientClosed())); connect(container, SIGNAL(error(int)), SLOT(showClientError(int))); } void Embedder::swallow() { QString id = input->text(); if (id.left(2) != "0x") { container->embed(input->text().toInt(), false); } else { bool ok; int intid = id.mid(2).toInt(&ok, 16); qDebug("embedding %i", intid); container->embed(intid, false); } } void Embedder::reject() { container->discardClient(); display->setText("Ready."); } void Embedder::showClientEmbedded() { display->setText("Client is embedded."); } void Embedder::showClientClosed() { display->setText("Client closed."); } void Embedder::showClientError(int err) { switch (err) { case QtXEmbedContainer::Unknown: display->setText("Unknown error."); break; case QtXEmbedContainer::InvalidWindowID: display->setText("Invalid window ID."); break; } }
TEMPLATE = app INCLUDEPATH += . include(../../src/qtxembed.pri) HEADERS += swallow.h SOURCES += main.cpp swallow.cpp
#include <qapplication.h> #include "swallow.h" int main(int argc, char *argv[]) { QApplication app(argc, argv); Embedder window; app.setMainWidget(&window); window.show(); return app.exec(); }
Copyright © 2003 Trolltech | Trademarks | Qt Solutions
|