连接主窗口类外的插槽

时间:2013-03-31 09:28:10

标签: c++ qt qt4 signals-slots

我有一些GUI(显示使用QTableWidget的帐单),我已在MainWindow文件的checkout.cpp类之外实现了。我在连接checkout.cpp内的插槽时遇到问题。  由于MainWindow类继承自QMainWindow,因此我可以将相应的槽函数与此对象相关联。

如何在不继承Q CheckOutMainWindow的{​​{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(); }

1 个答案:

答案 0 :(得分:1)

如果要使用插槽和信号,则需要在类的私有部分添加Q_OBJECT并从QObject继承。

您可以使用的任何Qt类都继承自QObject,因此,如果您继承自QWidget,那么您也会继承QObject

class CheckOut : public QObject {
Q_OBJECT
...
}

如果您将QObject*父级传递给CheckOut构造函数,则可能还需要与该父级构建QObject子对象:

CheckOut::CheckOut(QObject* parent) : QObject(parent) { ... }