开发者

Qt, Signals without naming?

开发者 https://www.devze.com 2022-12-15 14:56 出处:网络
开发者_如何学运维Is there any way to use the signals without MOC and without the connecting via names? My one problem with Qt is that you have something like
开发者_如何学运维

Is there any way to use the signals without MOC and without the connecting via names? My one problem with Qt is that you have something like

this->connect(this->SaveBtn, SIGNAL(click()), SLOT(SaveClicked()));

And there is no error detection to tell that is wrong other then finding out the button doesn't work or searching through their documentation to find out the signal doesn't exist. Also it seems pointless and a waste of cycles to connect via names instead of classes.


There is error detection, the connect function returns false when it fails to connect, and a warning is output on standard error (or, on Windows, to the weird place which DebugView reads from). Also you can make these warnings into fatal errors by setting QT_FATAL_WARNINGS=1 in your environment.

It's not pointless to connect by name. For example, it means that connections can be established where the signal/slot names are generated at runtime.


Other than the fact that signals are just methods, no I don't think you can use them like they are intended without the intermediate MOC step. It is a pain when you mess up the connection and there is no flag raised. What does your last sentence mean? Can you elaborate what your problem is? Signals/Slots is not a perfect system, I don't think a perfect system exits, but it is pretty intuitive and has worked well for me.


No, there is no real way around that.

You'll have to use MOC and connect via names. But with time you'll find out that it "grows on you" and won't really bother you. Train yourself to add code in small snippets each time, testing that what you added works, and you'll have no trouble with this.


I normally Practice following style of coding,

m_pCancelPushButton = new QPushButton(tr("Cancel"));
m_pCancelPushButton->setObjectName("CancelButton");


//MetaObject Connections
QMetaObject::connectSlotsByName (this);

This enable me to write code

void Class_name::on_CancelButton_clicked()
{
//Do your job here.
    reject();
}

I hope it will help you.

Thanks, Rahul

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号