#include <QApplication>#include <QLabel>#include <QPushButton>#include <QWidget>#include <QVBoxLayout>void olo(QPushButton *but) { but->setText("New Text");}int main(int argc, char *argv[]){ QApplication a(argc, argv); QWidget *win = new QWidget; win->setWindowTitle("Text"); QHBoxLayout *lo = new QHBoxLayout; QPushButton *but = new QPushButton("Exit"); QObject::connect(but, SIGNAL(click()), but, SLOT(olo(but))); lo->addWidget(but); win->setLayout(lo); win->show(); return a.exec();}
#include <QPushButton>class QPushButton2: public QPushButton{ Q_OBJECT public : QPushButton2(const QString& Text) : QPushButton(Text) {} public slots : void olo();};
#include "qpushbutton2.hpp"void QPushButton2::olo() { setText("New Text");}
#include <QApplication>#include <QLabel>#include <QWidget>#include <QVBoxLayout>#include "qpushbutton2.hpp"int main(int argc, char *argv[]){ QApplication a(argc, argv); QWidget *win = new QWidget; win->setWindowTitle("Text"); QHBoxLayout *lo = new QHBoxLayout; QPushButton2 *but = new QPushButton2("Exit"); QObject::connect(but, SIGNAL(clicked()), but, SLOT(olo())); lo->addWidget(but); win->setLayout(lo); win->show(); return a.exec();}
qmake -projectqmakenmake или make или mingw32-make
C++ (Qt)#ifndef WIDGET_H#define WIDGET_H #include <QtGui/QWidget> class QPushButton; class Widget : public QWidget{ Q_OBJECT public: Widget(QWidget *parent = 0); ~Widget(); void setLabels(const QString &text1, const QString &text2); private slots: void changeText(); private: QString _text1; QString _text2; static bool flag; QPushButton *_btn;}; #endif // WIDGET_H
C++ (Qt)#include "widget.h"#include <QPushButton>#include <QVBoxLayout> bool Widget::flag = false; Widget::Widget(QWidget *parent) : QWidget(parent){ _btn = new QPushButton; QVBoxLayout *mainLayout = new QVBoxLayout(this); mainLayout->addWidget(_btn); connect(_btn, SIGNAL(clicked()), this, SLOT(changeText())); } void Widget::setLabels(const QString &text1, const QString &text2){ _text1 = text1; _text2 = text2; _btn->setText(_text1);} void Widget::changeText(){ _btn->setText(flag ? _text1 : _text2); flag = !flag;} Widget::~Widget(){ }
C++ (Qt)#include <QtGui>#include "widget.h" int main(int argc, char *argv[]){ QApplication a(argc, argv); Widget w; w.setLabels("text1", "text2"); w.show(); return a.exec();}