0

我有一些 GUI(显示使用中的账单QTableWidget),我在文件中的MainWindow类之外实现了这些。checkout.cpp我在连接checkout.cpp. 由于MainWindow该类继承自QMainWindow,因此我可以将适当的槽函数与 this 对象相关联。

如何在CheckOut不继承自 QMainWindow或的类中执行此操作QWidget

编辑(代码):

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();
}

编辑(错误):

/home/sudeep/Desktop/mesonero project/mesonero-build-desktop-Qt_4_8_1_in_PATH__System__Release/../mesonero/checkout.cpp:29: error: no matching function for call to 'QObject::connect(QPushButton*&, const char [11], CheckOut* const, const char [8])'

4

1 回答 1

1

当您想使用插槽和信号时,您需要Q_OBJECT在类的私有部分中添加并继承自QObject.

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

class CheckOut : public QObject {
Q_OBJECT
...
}

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

CheckOut::CheckOut(QObject* parent) : QObject(parent) { ... }
于 2013-03-31T09:41:01.910 回答