• 6786阅读
  • 4回复

全局变量的问题 [复制链接]

上一主题 下一主题
离线甲骨文
 
只看楼主 正序阅读 楼主  发表于: 2008-12-20
— 本帖被 XChinux 执行加亮操作(2008-12-23) —
大家好,我是QT新手。

connect(okButton, SIGNAL(clicked()), this, SLOT(okBtnClicked());

我需要传一个参数过去,但是clicked() 与okBtnClicked()两个需要一样
那我应该如何传这个参数过去呢?

下面是我的cpp代码

#include <QString>
#include <QtGui>
#include <QMessageBox>
#include <QtDebug>

#include "doServe.h"

CDoServe::CDoServe(QWidget *parent)
    : QDialog(parent)
{
    db = QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("localhost");
    db.setDatabaseName("test");
    db.setUserName("root");
    db.setPassword("123");
    db.open();
}

void CDoServe::doAction(int id_type)
{
    QSqlQuery query;
    QString control_id = getControlId();
    QString box_id = getBoxId();

    switch(id_type){
    case 3:
       showDialog();
    break;
    default:
            query.exec(QString("INSERT INTO `Bells` (id_type, service_count, box_id, control_id, start_time) VALUES('%1', 1, '%2', '%3', now());").arg(id_type).arg(box_id).arg(control_id));
    break;
    }
}

void
CDoServe::showDialog()
{
    dialog = new QDialog;

    QLabel *shareQuestion = new QLabel(tr("请输入您的份额:"));
    QLabel *shareUnit = new QLabel(tr("份"));
    shareEdit = new QLineEdit;
    okButton = new QPushButton(tr("确定"));
    cancelButton = new QPushButton(tr("取消"));

    iceLayout = new QGridLayout;
    iceLayout->addWidget(shareQuestion, 0, 0, 1, 2);
    iceLayout->addWidget(shareEdit, 0, 2, 1, 1);
    iceLayout->addWidget(shareUnit, 0, 3, 1, 2);
    iceLayout->addWidget(okButton, 1, 1, 1, 1);
    iceLayout->addWidget(cancelButton, 1, 3, 1, 1);
   
    dialog->setLayout(iceLayout);
    dialog->setWindowTitle(tr("加冰块"));
    dialog->resize(250, 200);
    dialog->show();

    connect(okButton, SIGNAL(clicked()), this, SLOT(okBtnClicked()));
    connect(cancelButton, SIGNAL(clicked()), dialog, SLOT(close()));
}

void
CDoServe::okBtnClicked()
{
    QSqlQuery query;
    QString box_id = getBoxId();
    QString control_id = getControlId();
    int count = shareEdit->text().trimmed().toInt();
    if(count < 1 || count > 10)
    {       
        shareEdit->clear();
    }
    else
    {
        query.exec(QString("INSERT INTO `Bells` (id_type, serveice_count, box_id, control_id, start_time) VALUES('%1', '%2', '%3', '%4',now());").arg(id_type).arg(count).arg(box_id).arg(control_id));
        dialog->close();
      // cancelButton->setEnabled(shareEdit->text().isEmpty());
    }
}

void
CDoServe::warning()
{

}

QString
CDoServe::getControlId()
{
    QString box_id = getBoxId();
    QSqlQuery query;
    query.exec("SELECT `control_id` FROM `Place` WHERE `id` = '"+box_id+"'  LIMIT 1");
    query.next();
    return  query.value(0).toString();
}

QString
CDoServe::getBoxId()
{
    return "R0201";
}
[ 此贴被甲骨文在2008-12-20 16:44重新编辑 ]
离线wd007

只看该作者 4楼 发表于: 2008-12-25
应该可以的
欢迎访问我的博客,一起学习提高
http://blog.csdn.net/qter_wd007
离线gery_sunjian
只看该作者 3楼 发表于: 2008-12-25
2楼意思:
connect(okButton, SIGNAL(clicked()), this, SLOT(tempBtnClicked());

void 类::tmpBtnClicked()
{
      QString str("hello");
      okBtnClicked(str);      //传入参数
}
离线stdjgwyc
只看该作者 2楼 发表于: 2008-12-22
没特别清楚意思~
增加一个类成员,专门保存okButton点击之需要传递的参数,行不
QT交流群群号:2906359,(200人大群,持续升级)
离线nmiirq

只看该作者 1楼 发表于: 2008-12-21
嘻嘻,我能想到的就是折中的一个办法了,你是想连接这两个东东:connect(okButton, SIGNAL(clicked()), this, SLOT(okBtnClicked());但又要传递变量过去,所以先重写一个类继承自QPushButton类,定义其一个新的slot比如叫: tmpSlot()和一个新的SINGAL:newSig(QString strSend);
然后重新定义你原来的SLOT: okBtnClicked()为okBtnClicked(QString strRecv);
这样通过下面的几句话,你就可以达到你要的效果了!
在okButton的文件里:
connect(okButton, SIGNAL(clicked()), this, SLOT(tmpSlot());
void tmpSlot()
{
  QString str("hello,test");
  emit  newSig(str);
}
在原来的文件里:
connect(okButton,SIGNAL(newSig(QString strSend)),this,SLOT(okBtnClicked(QString strRecv));

不知有没有更方便的方法,大家指点一下下!
快速回复
限100 字节
 
上一个 下一个