Try out the following example by saving it as a single file called main.cpp and compiling and executing it.
#include <QtGui>
{
Q_OBJECT
public:
{
QLabel* lb
= new QLabel("Enter text below and press <b>Enter</b>");
lb->setWordWrap(true);
connect(m_le, SIGNAL(returnPressed()), this, SLOT(display())); // What you need
// layout
lo->addWidget(lb);
lo->addWidget(m_le);
lo->addWidget(m_tb);
// set central widget
setCentralWidget(cw);
}
private slots: // display() is a declared a slot!!!
void display()
{
if (m_le->text() == "")
QMessageBox::warning(this,
"Empty string",
"You have to enter something!");
else
m_tb->setText(m_le->text());
}
private:
};
int main(int argc, char *argv[])
{
MainWindow mw;
mw.show();
return app.exec();
}
#include "main.moc"
#include <QtGui>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget* parent=0) : QMainWindow(parent)
{
QWidget* cw = new QWidget;
QVBoxLayout* lo = new QVBoxLayout(cw);
QLabel* lb = new QLabel("Enter text below and press <b>Enter</b>");
lb->setWordWrap(true);
m_le = new QLineEdit;
connect(m_le, SIGNAL(returnPressed()), this, SLOT(display())); // What you need
m_tb = new QTextBrowser;
// layout
lo->addWidget(lb);
lo->addWidget(m_le);
lo->addWidget(m_tb);
// set central widget
setCentralWidget(cw);
}
private slots: // display() is a declared a slot!!!
void display()
{
if (m_le->text() == "")
QMessageBox::warning(this, "Empty string", "You have to enter something!");
else
m_tb->setText(m_le->text());
}
private:
QLineEdit* m_le;
QTextBrowser* m_tb;
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow mw;
mw.show();
return app.exec();
}
#include "main.moc"
To copy to clipboard, switch view to plain text mode
Bookmarks