Tôi không dùng Design để tạo Ui nhưng tới bước xử lý bấm vào button thì có vẻ mọi chuyện không suôn sẻ như lúc dùng Design. Tôi đã xem qua moc file của Qt tạo ra trong cả 2 trường hợp dùng và không dùng Design. Cả 2 có vẻ như giống hệt nhau.
Đây là code của tôi:
main.cpp:
#include <QApplication>
#include "TestSomething.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
TestSomething* ui = new TestSomething();
ui->show();
return app.exec();
}
TestSomething.h:
#pragma once
#include <QWidget>
class QPushButton;
class QLabel;
class TestSomething : public QWidget
{
Q_OBJECT
public:
TestSomething(QWidget* parent = NULL);
~TestSomething();
private slots:
void on_button_clicked();
private:
QPushButton* button;
QLabel* label;
};
TestSomething.cpp
#include "TestSomething.h"
#include <QPushButton>
#include <QLabel>
TestSomething::TestSomething(QWidget *parent) : QWidget(parent)
{
button = new QPushButton("button", this);
label = new QLabel("Label");
}
TestSomething::~TestSomething()
{
delete this;
}
void TestSomething::on_button_clicked(){
label->show();
}
Tôi hiểu về signal và slot nhưng trong trường hợp này tôi không muốn dùng connect.
Xin mọi người giải thích giùm.
Nếu có thể thì xin mọi người chỉ giáo thêm về cách xử lý event trong Qt kiểu giống như setOnClickListener trong Java.
Cảm ơn rất nhiều!