• 5695阅读
  • 4回复

[提问]信号和槽的问题 [复制链接]

上一主题 下一主题
离线lanlovehua
 
只看楼主 倒序阅读 楼主  发表于: 2011-04-20
代码是我写的模拟手机拨号的界面
  1. #include <QtGui/QApplication>
  2. #include <QtGui/QWidget>
  3. #include <QtGui/QLabel>
  4. #include <QtGui/QPushButton>
  5. #include <QTextEdit>
  6. #define MWINW   240 //主窗口宽度
  7. #define MWINH   320 //主窗口高度
  8. #define KEYW    80 //按钮宽度
  9. #define KEYH    40 //按钮高度
  10. class MyPhoneWin : public QWidget
  11. {
  12.     public:
  13.         MyPhoneWin();
  14.     private:
  15.         QPushButton *b1;
  16.         QPushButton *b2;
  17.         QPushButton *b3;
  18.         QPushButton *b4;
  19.         QPushButton *b5;
  20.         QPushButton *b6;
  21.         QPushButton *b7;
  22.         QPushButton *b8;
  23.         QPushButton *b9;
  24.         QPushButton *b0;
  25.         QPushButton *bA;
  26.         QPushButton *bB;
  27.         QPushButton *bBack;
  28.         QPushButton *bOK;
  29.         QLabel *label;
  30.         QTextEdit *line;
  31. };
  32. MyPhoneWin::MyPhoneWin()
  33. {
  34.     setGeometry(100, 100, MWINW, MWINH);
  35.     setMinimumSize(MWINW, MWINH);
  36.     setMaximumSize(MWINW, MWINH);
  37.     label = new QLabel(this);
  38.     label->setGeometry(0, 0, MWINW, 120);
  39.     label->setText("This is the first line.\nThis is the second line.");
  40.     line = new QTextEdit(this);
  41.     line->setGeometry(0, 0, MWINW, 100);
  42.     b1 = new QPushButton("1", this);
  43.     b2 = new QPushButton("2", this);
  44.     b3 = new QPushButton("3", this);
  45.     b4 = new QPushButton("4", this);
  46.     b5 = new QPushButton("5", this);
  47.     b6 = new QPushButton("6", this);
  48.     b7 = new QPushButton("7", this);
  49.     b8 = new QPushButton("8", this);
  50.     b9 = new QPushButton("9", this);
  51.     b0 = new QPushButton("0", this);
  52.     bA = new QPushButton("*", this);
  53.     bB = new QPushButton("#", this);
  54.     bOK = new QPushButton("OK", this);
  55.     bBack = new QPushButton("Back", this);
  56.     b1->setGeometry(0,          121, KEYW, KEYH);
  57.     b2->setGeometry((1*KEYW-1), 121, KEYW, KEYH);
  58.     b3->setGeometry((2*KEYW-1), 121, KEYW, KEYH);
  59.     b4->setGeometry(0,          (121+KEYH*1), KEYW, KEYH);
  60.     b5->setGeometry((1*KEYW-1), (121+KEYH*1), KEYW, KEYH);
  61.     b6->setGeometry((2*KEYW-1), (121+KEYH*1), KEYW, KEYH);
  62.     b7->setGeometry(0,          (121+KEYH*2), KEYW, KEYH);
  63.     b8->setGeometry((1*KEYW-1), (121+KEYH*2), KEYW, KEYH);
  64.     b9->setGeometry((2*KEYW-1), (121+KEYH*2), KEYW, KEYH);
  65.     bA->setGeometry(0,          (121+KEYH*3), KEYW, KEYH);
  66.     b0->setGeometry((1*KEYW-1), (121+KEYH*3), KEYW, KEYH);
  67.     bB->setGeometry((2*KEYW-1), (121+KEYH*3), KEYW, KEYH);
  68.     bOK->setGeometry(0,          (121+KEYH*4), KEYW, KEYH);
  69.     bBack->setGeometry((2*KEYW-1), (121+KEYH*4), KEYW, KEYH);
  70.     connect(b1, SIGNAL(clicked()), line, SLOT(setText("1")));
  71.     connect(b2, SIGNAL(clicked()), line, SLOT(setText("2")));
  72.     connect(b3, SIGNAL(clicked()), line, SLOT(setText("3")));
  73.     connect(bBack, SIGNAL(clicked()), line, SLOT(clear()));
  74. }
  75. int main(int argc, char *argv[])
  76. {
  77.     QApplication app(argc, argv);
  78.     MyPhoneWin w;
  79.     w.show();
  80.     return app.exec();
  81. }


89, 90, 91行是这样的:
connect(b1, SIGNAL(clicked()), line, SLOT(setText("1")));
    connect(b2, SIGNAL(clicked()), line, SLOT(setText("2")));
    connect(b3, SIGNAL(clicked()), line, SLOT(setText("3")));
但是按键后并没有执行setText,请问大侠这是怎么回事?
谢谢了。
离线ppdayz

只看该作者 1楼 发表于: 2011-04-20
槽的参数要和信号的参数保持一致,个数也可以少于信号的参数的个数
你这里信号没有参数,SLOT有参数,当然不对
离线lanlovehua
只看该作者 2楼 发表于: 2011-04-21
clicked()是没有参数的。
那请问怎么改啊?
离线lanlovehua
只看该作者 3楼 发表于: 2011-04-21
改成了
clicked(TRUE)

可是还是不行啊。
离线dbzhang800

只看该作者 4楼 发表于: 2011-04-21
引用第3楼lanlovehua于2011-04-21 14:36发表的  :
改成了
clicked(TRUE)
可是还是不行啊。

先看看manual或例子吧,乱试不是解决问题的办法。
快速回复
限100 字节
 
上一个 下一个