作者liaommx (Orz)
看板C_and_CPP
標題[語法] QT call function 帶參數?
時間Wed Nov 10 19:07:44 2010
我有一個按鈕,
希望按下去以後能讓程式中的某個圖片移動,
我知道怎麼讓圖片移動,
知道怎麼讓按鈕call signal,
可是SLOT的地方我怎麼寫都不對,
另外就是call function帶參數的地方也怎麼寫都不對,
有沒有人可以幫忙提點我一下?
主要是希望點了那個按鈕之後,
呼叫某個子函數,
該子函數會自動把承接到的label name所代表的圖片移動
如下列程式碼,
按下按鈕後,會把bird_1這個label的名字傳送到button_temp這個function,
然後button_temp這個function會將傳來的子函數所代表的label移動,
請大家協助我一下,
QT新手,也想說試著不要用designer去做UI,
所以學的比較不好,
程式碼如下,
=======
下面這是widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QtGui/QWidget>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
public slots:
void button_temp(int value);
};
#endif // WIDGET_H
============
下面這是main.cpp
#include <QApplication>
#include "widget.h"
#include <QLabel>
#include <QPixmap>
#include <QPropertyAnimation>
#include <qpushbutton.h>
#include "qmessagebox.h"
#include <QString>
int main(int argc,char *argv[]){
QApplication app(argc,argv);
Widget *w=new Widget();
w->resize(300,400);
QPixmap birdimg=QPixmap("home.bmp").scaled(142,69);
QLabel *bird_1=new QLabel(w);
bird_1->setPixmap(birdimg);
QPushButton *temp=new QPushButton("w",w);
QObject::connect(temp,SIGNAL(clicked()),w,SLOT(button_temp(bird_1)));
temp->show();
w->show();
return app.exec();
}
void Widget::button_temp(QLabel bird_1){
QPropertyAnimation *anim1=new QPropertyAnimation(bird_1, "pos");
anim1->setDuration(1000);
anim1->setStartValue(QPoint(0, 360));
anim1->setEndValue(QPoint(200,150 ));
anim1->setEasingCurve(QEasingCurve::OutBounce);
anim1->start();
bird_1->move(100,100);
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 118.169.228.133
→ uranusjr:你直接把按鈕的 signal 接到 button_temp(int) 嗎? 這樣 11/10 19:50
→ uranusjr:不行, slot 不能擁有 signal 沒給的參數, 你要另寫一個 11/10 19:51
→ uranusjr:中繼 slot 來接 signal, 在裡面增加參數後再 call 11/10 19:51
→ liaommx:所以中繼slot再去 button_temp(*Qlabel) 這樣嗎? 11/10 21:35
→ Bencrie:不靠IDE沒什麼不好啊 你可以了解整個build過程呢 XD 11/10 22:39
推 ables:可以用QSignalMapper來中繼... 11/10 22:40
→ liaommx:我現在可以傳一般值,可是要怎麼讓中繼slot知道變數是甚麼 11/11 09:02
→ liaommx:就是,要怎麼將object name傳給function使用 11/11 09:03
→ uranusjr:你讓 slot 直接讀 label 的值不就好了... 11/11 10:11
→ liaommx:不太懂?可以給我多一點提示嗎? 11/11 11:06