QComboBox未连接 我在QT4.8中用C++编程。我想有一个下拉菜单,在这里我可以选择一个选项,然后它将运行一个exe与菜单上的选定项目作为exe的选项
这是我的密码:QComboBox未连接 我在QT4.8中用C++编程。我想有一个下拉菜单,在这里我可以选择一个选项,然后它将运行一个exe与菜单上的选定项目作为exe的选项,c++,qt,qcombobox,C++,Qt,Qcombobox,这是我的密码: #ifndef GUI_H #define GUI_H #include <QDialog> #include <QtGui> class QLabel; class QLineEdit; class QPushButton; class gui : public QDialog { Q_OBJECT public: gui(QWidget *parent = 0); public slots: void gui::on_
#ifndef GUI_H
#define GUI_H
#include <QDialog>
#include <QtGui>
class QLabel;
class QLineEdit;
class QPushButton;
class gui : public QDialog
{
Q_OBJECT
public:
gui(QWidget *parent = 0);
public slots:
void gui::on_go_clicked();
private:
QLabel *label1;
QLabel *label2;
QLineEdit *lineEdit;
QPushButton *goButton;
QComboBox cb;
};
#endif
和.cpp文件:
#include <QtGui>
#include <QApplication>
#include <QComboBox>
#include "gui.h"
#include <vector>
gui::gui(QWidget *parent) : QDialog(parent)
{
label1 = new QLabel(tr("Insert Name (Optional):"));
label2 = new QLabel(tr("Class Name (Required):"));
lineEdit = new QLineEdit;
goButton = new QPushButton(tr("&Go"));
goButton->setDefault(true);
connect(goButton, SIGNAL(clicked()), this, SLOT(on_go_clicked()));
QComboBox *cb = new QComboBox();
cb->addItem("Hello", "1");
cb->addItem("Test", "2");
QHBoxLayout *hLayout1 = new QHBoxLayout;
hLayout1->addWidget(label1);
hLayout1->addWidget(lineEdit);
QHBoxLayout *hLayout2 = new QHBoxLayout;
hLayout2->addWidget(label2);
hLayout2->addWidget(cb);
QHBoxLayout *hLayout3 = new QHBoxLayout;
hLayout3->addWidget(goButton);
hLayout3->addStretch();
QVBoxLayout *vLayout = new QVBoxLayout;
vLayout->addLayout(hLayout1);
vLayout->addLayout(hLayout2);
vLayout->addWidget(cb);
vLayout->addLayout(hLayout3);
setLayout(vLayout);
setWindowTitle(tr("TEST"));
setFixedHeight(sizeHint().height());
}
void gui::on_go_clicked()
{
QMessageBox::information(this, "ASDF", cb.currentText());
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
gui *stuff = new gui;
stuff->show();
return app.exec();
}
现在我只是想弄清楚如何使用QComboBox,但它不起作用。我的代码可以编译,但当我运行它时,我得到了Object::connect:没有这样的插槽gui::on\u go\u clicked
我做的正是教程上说的。我不明白为什么这不起作用。我想知道你为什么编写代码甚至编译。没有“会员在点击时的额外资格”。
删除gui::从标题中单击的“开始”中删除。我想知道为什么要编写编译代码。没有“会员在点击时的额外资格”。 删除gui::从标题中单击的“开始”。删除gui::: 删除gui:::
您有两个引用的QComboBox对象 第一个是班级层面:
class gui : public QDialog{
Q_OBJECT
public:
gui(QWidget *parent = 0);
public slots:
void gui::on_go_clicked();
private:
QLabel *label1;
QLabel *label2;
QLineEdit *lineEdit;
QPushButton *goButton;
QComboBox cb; // <<<=== class-level automatic object
};
此外,完成此操作后,还需要修改插槽,以便使用指针解引用操作符访问文本函数
您有两个引用的QComboBox对象 第一个是班级层面:
class gui : public QDialog{
Q_OBJECT
public:
gui(QWidget *parent = 0);
public slots:
void gui::on_go_clicked();
private:
QLabel *label1;
QLabel *label2;
QLineEdit *lineEdit;
QPushButton *goButton;
QComboBox cb; // <<<=== class-level automatic object
};
此外,完成此操作后,还需要修改插槽,以便使用指针解引用操作符访问文本函数
拥有额外的GUI::部分不是C++中的错误,它与Qt的信号和时隙系统有关,它根本不理解它们是同一个。AFAIK,在某些编译器(例如MSVC++)中,可以使用额外的资格来编写类似的代码。但是它不是标准的。代码编译是因为QObject::connect接受char*参数。编译时没有检查。文件可在此处找到Lochemage、segfault以及信号/插槽连接不工作的原因。但是使用gcc,您将无法编译此代码。我想说,你的行为很好。这样的错误很容易用复制粘贴来实现,但不容易被发现。如果我摆脱GUI::我不能在槽中使用QCOMBOX。拥有额外的GUI::部分不是C++中的错误,它与Qt的信号和时隙系统有关,它根本不理解它们是同一个。在某些编译器中,如MSVC++中,可以使用额外的限定来编写这样的代码。但是它不是标准的。代码编译是因为QObject::connect接受char*参数。编译时没有检查。文件可在此处找到Lochemage、segfault以及信号/插槽连接不工作的原因。但是使用gcc,您将无法编译此代码。我想说,你的行为很好。复制粘贴很容易产生这种错误,但spottet就不那么容易了。如果我去掉gui:,我就不能在插槽中使用QCOMBOX。此外,在构造函数中,您将QCOMBOX分配给hLayout2,然后再重新分配给vLayout。你需要决定你想要它的布局。哦,那不是故意的。我想我只是在重新安排工作的时候把它忘在了里面。另外,在构造函数中,你把你的QComboBox分配给了hLayout2,然后又把它重新分配给了vLayout。你需要决定你想要它的布局。哦,那不是故意的。我想我只是把它忘在里面了,当时我正在重新安排事情,试图让它发挥作用。
gui::gui(QWidget *parent) : QDialog(parent){
...
QComboBox *cb = new QComboBox(); // <<<=== function-level pointer using the same name
// as the class-level automatic object
cb = new QComboBox();
void gui::on_go_clicked(){
QMessageBox::information(this, "ASDF", cb->currentText());
}