创建了工具栏,添加了第三个QAction时,在MainWindow类里
RectAction[2] = new QAction(tr("&Text"), this);
RectAction[2]->setStatusTip(tr("Inpute a Text."));
RectAction[2]->setIcon(QIcon("icons/encoding.png"));
connect(RectAction[2],SIGNAL(triggered()),this,SLOT(Text_selected()));
m_ptoolbar->addAction(RectAction[2]);/////
m_paction = RectAction[2];////m_paction 为全局变量,为了传到另一个类里
还是在MainWindow里,Text_selected槽函数
void MainWindow::Text_selected()
{
emit createText();//信号能进这里,发到另一个类里面
}
另一个类
MyPainter::MyPainter()//MyPainter里面做信号处理
{
connect(m_paction,SIGNAL(createText()),this,SLOT(recvtocreateText()),Qt::DirectConnection);//这里连接createText信号!!!
}
void MyPainter::recvtocreateText()///这个槽函数不被执行!!!!!
{
QLineEdit *edit = new QLineEdit(this);
edit->move(QPoint());//
edit->show();
}
报警告说:Object::connect: No such signal QAction::createText() in MyPainter.cpp:41
可是我的程序有这个信号啊!!!!!
------解决方案--------------------
在MainWindow的构造函数里new出一个MyPainter对象,然后添加信号槽:
connect(mainwindow, SIGNAL(createText()),m_paction, SLOT(recvtocreateText()));
具体代码根据你程序改下,我随手写的