Qt/Widgets/LineEdit
From ProgrammingExamples
main.cpp
#include <QApplication> #include "form.h" int main(int argc, char *argv[]) { QApplication app(argc, argv); MyForm form; form.show(); return app.exec(); }
form.cpp
#include <QtGui> #include "form.h" Form::Form(QWidget *parent) : QWidget(parent) { ui.setupUi(this); connect( this->ui.btnUpdate, SIGNAL( clicked() ), this, SLOT(btnUpdate_SetLabelText()) ); connect( this->ui.lineEdit, SIGNAL( returnPressed()), this, SLOT(btnUpdate_SetLabelText()) ); connect( this->ui.lineEdit, SIGNAL( editingFinished()), this, SLOT(btnUpdate_SetLabelText()) ); } void Form::btnUpdate_SetLabelText() { this->ui.label->setText(this->ui.lineEdit->text()); }
form.h
#ifndef FORM_H #define FORM_H #include "ui_form.h" class Form : public QWidget { Q_OBJECT public: Form(QWidget *parent = 0); public slots: void btnUpdate_SetLabelText(); private: Ui::Form ui; }; #endif
main.ui
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>Form</class> <widget class="QWidget" name="Form"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>400</width> <height>300</height> </rect> </property> <property name="windowTitle"> <string>Form</string> </property> <widget class="QPushButton" name="btnUpdate"> <property name="geometry"> <rect> <x>170</x> <y>120</y> <width>91</width> <height>28</height> </rect> </property> <property name="text"> <string>Update</string> </property> </widget> <widget class="QLabel" name="label"> <property name="geometry"> <rect> <x>170</x> <y>220</y> <width>61</width> <height>18</height> </rect> </property> <property name="text"> <string>TextLabel</string> </property> </widget> <widget class="QLineEdit" name="lineEdit"> <property name="geometry"> <rect> <x>170</x> <y>50</y> <width>113</width> <height>27</height> </rect> </property> </widget> </widget> <resources/> <connections/> </ui>
CMakeLists.txt
cmake_minimum_required(VERSION 2.6) PROJECT(LineEdit) FIND_PACKAGE(Qt4 REQUIRED) INCLUDE(${QT_USE_FILE}) QT4_WRAP_UI(UISrcs form.ui) QT4_WRAP_CPP(MOCSrcs form.h) include_directories(${include_directories} ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}) ADD_EXECUTABLE(LineEdit main.cpp form.h form.cpp ${MOCSrcs} ${UISrcs}) TARGET_LINK_LIBRARIES(LineEdit ${QT_LIBRARIES})