【问题标题】:invalid use of non-static datamember Ui_Mainwindow::(Pushbutton) in Qt在 Qt 中无效使用非静态数据成员 Ui_Mainwindow::(Pushbutton)
【发布时间】:2015-05-12 19:41:15
【问题描述】:

我已经定义了一些QTableWidget,使用继承和Qt Creator的一些按钮。

现在我正在尝试将来自按钮的信号连接到来自QTableWidget 的插槽。

main() 中,我已经定义了自定义(重新定义)QTableWidget 的 Food 对象:

Table Food(&w,3,700,50,610,getdata_foodtabel());

在下一行中,我尝试将它与我的QPushButton object(orderButton) 连接起来:

QObject::connect(Ui::MainWindow::orderButton, SIGNAL(w.on_orderButton_clicked()),&Food, SLOT(Food.get_number()));

在这一行中我得到了这个错误:

 error: invalid use of non-static data member 'Ui_MainWindow::orderButton'
 QPushButton *orderButton;
              ^

而且get number也是Table类中自定义的public slot

谁能告诉我为什么会出现这个错误? 我应该怎么做才能解决它?

我的表的构造函数 int Table.cpp:

Table::Table(QWidget *Parent,int row,int X,int Y,int height,std::vector     <std::vector <std::string > > food)
  :QTableWidget(food.size(),3,Parent)
{

this->setGeometry(QRect(X,Y,321,height));
this->setStyleSheet(QStringLiteral("background-color: rgb(206, 255, 255);"));
this->row=food.size();
for(int i=0;i<this->row;i++)
        this->food.push_back(food[i]);
for (int i=0;i<this->row;i++)
{
  //  this->food[i][0]=names[i];
   // this->food[i][1]=costs[i];
    this->setCellWidget(i,0,new label(QString::fromStdString(food[i][0])));
    this->setCellWidget(i,1,new label (QString::fromStdString(food[i][1])));
    this->setCellWidget(i,2,new QSpinBox);
}

 }

和我的主要功能:

 int main(int argc, char *argv[])
 {
    QApplication a(argc, argv);
    MainWindow w;
    Table Drinks(&w,3,40,50,280,getdata_drinktabel());
       Drinks.setHorizontalHeaderLabels(QString("نوشیدنی;قیمت;تعداد").split(";"));
       Table Desert(&w,3,40,380,280,getdata_desertabel());
        Desert.setHorizontalHeaderLabels(QString("دسر;قیمت;تعداد").split(";"));
        Table Appetizer(&w,3,370,50,610,getdata_beforetabel());
        Appetizer.setHorizontalHeaderLabels(QString("پیش غذا;قیمت;تعداد").split(";"));
        Table* Food=new Table(&w,3,700,50,610,getdata_foodtabel());
        Food->setHorizontalHeaderLabels(QString(" غذا;قیمت;تعداد").split(";"));
        Food->setStyleSheet(QStringLiteral("background-image: url(:/soltani-edited1.jpg);"));
        QObject::connect(Ui::MainWindow::orderButton, SIGNAL(clicked()),Food, SLOT(Food.get_number()));
        w.show();
    return a.exec();
}

【问题讨论】:

  • 没有更多代码很难说,但我猜你有一个类 MainWindow 有一个成员 orderButton。您似乎正在尝试使用 MainWindow::orderButton 作为连接参数,而不是 specific Mainwindows orderButton
  • 正确的语法应该是 QObject::connect(orderButton, SIGNAL(clicked()),&amp;Food, SLOT(get_number()));connect(orderButton, &amp;QPushButton::clicked, &amp;Food, &amp;Table::get_number); 你在这里声明和使用 Food 的方式表明它是在堆栈上创建的,并且不会在创建它的方法中存活。尝试在堆上创建它。
  • @FrankOsterfeld 错误是关于 orderButton 的......而且我动态定义了 Food(你的意思是......对吗??)它没有用
  • 实际上我试图像这样处理'QTablewidget':'Ui::MainWindow::tableWidget',但我仍然收到此错误:'错误:非静态数据成员'Ui_MainWindow:的使用无效: :tablewidget''
  • dwcanillas 已经告诉您错误是什么,您必须连接一个 特定 按钮,例如 QObject::connect(&amp;w.pushbuttonname, SIGNAL(...,然后使用您的 pushButton 的名称

标签: c++ qt


【解决方案1】:

我用另一种方式修复了它...

我刚刚从QPushButton 继承了一个类,并将它的信号连接到Food 的插槽。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-12
    • 2023-03-12
    • 2013-10-14
    • 2015-06-26
    相关资源
    最近更新 更多