我有一些GUI(显示使用QTableWidget
的帐单),我已在MainWindow
文件的checkout.cpp
类之外实现了。我在连接checkout.cpp
内的插槽时遇到问题。
由于MainWindow
类继承自QMainWindow
,因此我可以将相应的槽函数与此对象相关联。
如何在不继承Q CheckOut
或MainWindow
的{{1}}课程中执行此操作?
修改(CODE):
QWidget
修改(ERROR):
/ CheckOut::CheckOut(string token)The CheckOut class does not inherit any other class. I am getting error: /home/sudeep/Desktop/mesonero project/mesonero-build-desktop-Qt_4_8_1_in_PATH__System__Release/../mesonero/management.cpp:29: error: no matching function for call to 'QObject::connect(QPushButton*&, const char [11], Management* const, const char [8])'
{ CustomerToken = token;
if(!findCustomer())
QMessageBox::critical(0,QObject::tr("Check Out"),"Invalid Customer Token");
else{
generateBill();
provideDiscount();
QPushButton *payButton = new QPushButton("Pay");
QObject::connect(payButton,SIGNAL(clicked()),this,SLOT(deleteCustomer()));
CustomerBill->layout()->addWidget(payButton);
}
}
void CheckOut::deleteCustomer()
{
DatabaseManager *dbm = DatabaseManager::Instance();
QSqlQuery query("DELETE FROM `Residing_Customer` WHERE Customer_Token = '"+QString::fromStdString(CustomerToken)+"'",dbm->db);
query.exec();
CustomerBill->close();
}
答案 0 :(得分:1)
如果要使用插槽和信号,则需要在类的私有部分添加Q_OBJECT
并从QObject
继承。
您可以使用的任何Qt类都继承自QObject
,因此,如果您继承自QWidget
,那么您也会继承QObject
。
class CheckOut : public QObject {
Q_OBJECT
...
}
如果您将QObject*
父级传递给CheckOut
构造函数,则可能还需要与该父级构建QObject
子对象:
CheckOut::CheckOut(QObject* parent) : QObject(parent) { ... }