今天發現qt程序在運行時命令行窗口會彈出下面的警告:
QMetaObject::connectSlotsByName: No matching signal for ...
但實際功能並沒有受影響。網上google了一圈,終於找到原因。
After a bit of sleuthing to find the source of all the Qt warnings, “QMetaObject::connectSlotsByName: No matching signal for …”, in our log window/file, here’s what I’ve found.
setupUi calls connectSlotsByName which parses the moc_ file in order to connect slots and signals. The moc_ file contains a list of all the slots for the class. The parser iterates over the list of slot names looking for the following pattern: on_objectName_signal , where on_objectName_signal is the name of the slot, objectName is the object name and signal is the signal . For example, if you have a slot named, on_doneButton_clicked(), the parser looks for an object named doneButton, a signal named clicked and then connects the on_doneButton_clicked() slot to QButton’s signal clicked().
If you follow this naming convention, you do no t need to call the connect() method, nor do you need to connect the signal via the Qt UI editor in VisualStudio. connectSlotsByName will automatically make the connection for you.
So, in order to get rid of the “No matching signal for …” warnings, we need to either follow this naming convention, or make sure none of our slot names begin with “on_”.
上面大概就是說:用VistalStudio里的QtEditer可以自動調用Qt中“connectSlotsByName”即“按空間名稱關聯槽”的方式進行關聯,對應的函數必須寫成“on_控件名_信號名”的格式;或者也可以通過connet函數人為顯式地將信號和槽關聯起來。但是,如果采用顯式connect的方法的同時,又將槽函數的名字起成了“on_控件名_信號名”的格式,那么就會在運行時彈出 “QMetaObject::connectSlotsByName: No matching signal for”的警告了!