0

嘿,我几乎整个晚上都在寻找答案,但找不到。我的 QT creator 应用程序中有以下错误。我将向您发布我的 mainwindow.h 、 main.cpp 和 mainwindow.cpp ,我找到了一些我必须重新生成一些文件的答案,因为编译器不会自己生成并找到一些命令(例如 qmake - tp vc projectname.pro)在帮助中,但它没有用:(所以请帮助我,我差点哭了:D我只想更改每个插槽的主窗口的中央小部件;)

错误

QObject::connect: No such slot MainWindow::scW(mainwindow1)
QObject::connect:  (receiver name: 'MainWindow')
QObject::connect: No such slot MainWindow::scW(mainwindow2)
QObject::connect:  (receiver name: 'MainWindow')
QObject::connect: No such slot MainWindow::scW(mainwindow3)
QObject::connect:  (receiver name: 'MainWindow')
QObject::connect: No such slot MainWindow::scW(mainwindow1)
QObject::connect:  (receiver name: 'MainWindow')
QObject::connect: No such slot MainWindow::scW(mainwindow2)
QObject::connect:  (receiver name: 'MainWindow')
QObject::connect: No such slot MainWindow::scW(mainwindow3)
QObject::connect:  (receiver name: 'MainWindow')
QObject::connect: No such slot MainWindow::scW(mainwindow1)
QObject::connect:  (receiver name: 'MainWindow')
QObject::connect: No such slot MainWindow::scw(mainwindow2)
QObject::connect:  (receiver name: 'MainWindow')
QObject::connect: No such slot MainWindow::scW(mainwindow3)
QObject::connect:  (receiver name: 'MainWindow')

主窗口.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

public slots:
    void scW(QWidget *widget);

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

主窗口.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

void MainWindow::scW(QWidget *widget){
   this->setCentralWidget(widget);
}

MainWindow::~MainWindow()
{
    delete ui;
}

主文件

#include "mainwindow.h"
#include <QApplication>
#include <QWidget>
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGridLayout>
#include <QLabel>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    MainWindow *w=new MainWindow;

        QWidget *mainwindow1=new QWidget;
        QWidget *mainwindow2=new QWidget;
        QWidget *mainwindow3=new QWidget;
        QGridLayout *gridlay1=new QGridLayout;
        QGridLayout *gridlay2=new QGridLayout;
        QGridLayout *gridlay3=new QGridLayout;
        QWidget *window11=new QWidget;
        QWidget *window21=new QWidget;
        QWidget *window31=new QWidget;
        QVBoxLayout *vblay1=new QVBoxLayout;
        QVBoxLayout *vblay2=new QVBoxLayout;
        QVBoxLayout *vblay3=new QVBoxLayout;
        QPushButton *button_eins1=new QPushButton("Menü 1");
        QPushButton *button_zwei1=new QPushButton("Menü 2");
        QPushButton *button_drei1=new QPushButton("Menü 3");
        QPushButton *button_eins2=new QPushButton("Menü 1");
        QPushButton *button_zwei2=new QPushButton("Menü 2");
        QPushButton *button_drei2=new QPushButton("Menü 3");
        QPushButton *button_eins3=new QPushButton("Menü 1");
        QPushButton *button_zwei3=new QPushButton("Menü 2");
        QPushButton *button_drei3=new QPushButton("Menü 3");

        vblay1->addWidget(button_eins1);
        vblay1->addWidget(button_zwei1);
        vblay1->addWidget(button_drei1);
        vblay2->addWidget(button_eins2);
        vblay2->addWidget(button_zwei2);
        vblay2->addWidget(button_drei2);
        vblay3->addWidget(button_eins3);
        vblay3->addWidget(button_zwei3);
        vblay3->addWidget(button_drei3);
        window11->setLayout(vblay1);
        window21->setLayout(vblay2);
        window31->setLayout(vblay3);

        gridlay1->addWidget(window11,0,0);
        gridlay2->addWidget(window21,0,0);
        gridlay3->addWidget(window31,0,0);

        //Untermenü1
        QWidget *window12=new QWidget;
        QVBoxLayout *vblay12=new QVBoxLayout;
        QLabel *label1=new QLabel("willkommen!");
        vblay12->addWidget(label1);
        window12->setLayout(vblay12);

        gridlay1->addWidget(window12,0,1);
        //Ende Untermenü1

        //Untermenü2
        QWidget *window22=new QWidget;
        QVBoxLayout *vblay22=new QVBoxLayout;
        QLabel *label2=new QLabel("Untermenü 1!");
        vblay22->addWidget(label2);
        window22->setLayout(vblay22);

        gridlay2->addWidget(window22,0,1);
        //Ende Untermenü

        //Untermenü3
        QWidget *window32=new QWidget;
        QVBoxLayout *vblay32=new QVBoxLayout;
        QLabel *label3=new QLabel("Untermenü 2!");
        vblay32->addWidget(label3);
        window32->setLayout(vblay32);

        gridlay3->addWidget(window32,0,1);
        //Ende Untermenü

        mainwindow1->setLayout(gridlay1);
        mainwindow2->setLayout(gridlay2);
        mainwindow3->setLayout(gridlay3);

        w->setCentralWidget(mainwindow1);

        QObject::connect(button_eins1,SIGNAL(clicked()),w,SLOT(scW(mainwindow1)));
        QObject::connect(button_zwei1,SIGNAL(clicked()),w,SLOT(scW(mainwindow2)));
        QObject::connect(button_drei1,SIGNAL(clicked()),w,SLOT(scW(mainwindow3)));
        QObject::connect(button_eins2,SIGNAL(clicked()),w,SLOT(scW(mainwindow1)));
        QObject::connect(button_zwei2,SIGNAL(clicked()),w,SLOT(scW(mainwindow2)));
        QObject::connect(button_drei2,SIGNAL(clicked()),w,SLOT(scW(mainwindow3)));
        QObject::connect(button_eins3,SIGNAL(clicked()),w,SLOT(scW(mainwindow1)));
        QObject::connect(button_zwei3,SIGNAL(clicked()),w,SLOT(scw(mainwindow2)));
        QObject::connect(button_drei3,SIGNAL(clicked()),w,SLOT(scW(mainwindow3)));
        w->show();

    return a.exec();
}

编辑

这是我与一个创建者和一个 qwidget 类的 3 个插槽编写的新代码,我添加到 main.cpp 的主窗口中。我仍然收到插槽映射的错误(a,b,c 不存在。请帮助: D 格力兹

#include "window1.h"
#include "window2.h"
#include "window3.h"
#include "window4.h"
#include <QPushButton>
#include <QSignalMapper>
#include <QVBoxLayout>
#include <QGridLayout>
#include <QWidget>
#include <QObject>
#include <QLabel>

window1::window1(QWidget *parent) :
    QWidget(parent)
{
    int a=1,b=2,c=3;
    QWidget *menu_win=new QWidget;
    window3 *cont_two_win=new window3();
    window4 *cont_three_win=new window4();
    QPushButton *buttone_one=new QPushButton("one");
    QPushButton *buttone_two=new QPushButton("two");
    QPushButton *buttone_three=new QPushButton("three");
    QVBoxLayout *vblay=new QVBoxLayout;
    QGridLayout *gridlay=new QGridLayout;
    QSignalMapper *signalmapper=new QSignalMapper();

    vblay->addWidget(buttone_one);
    vblay->addWidget(buttone_two);
    vblay->addWidget(buttone_three);

    menu_win->setLayout(vblay);
    gridlay->addWidget(menu_win,0,0);
   // gridlay->addWidget(cont_win,0,1);
    this->setLayout(gridlay);

    QObject::connect(buttone_one,SIGNAL(clicked()),signalmapper,SLOT(map()));
    QObject::connect(buttone_two,SIGNAL(clicked()),signalmapper,SLOT(map()));
    QObject::connect(buttone_three,SIGNAL(clicked()),signalmapper,SLOT(map()));

    signalmapper->setMapping(buttone_one,a);
    signalmapper->setMapping(buttone_two,b);
    signalmapper->setMapping(buttone_three,c);

    QObject::connect(signalmapper,SIGNAL(mapped(a)),this,SLOT(change_cont_one(&gridlay)));
    QObject::connect(signalmapper,SIGNAL(mapped(b)),this,SLOT(change_cont_two(&gridlay)));
    QObject::connect(signalmapper,SIGNAL(mapped(c)),this,SLOT(change_cont_three(&gridlay)));
}

void window1::change_cont_one(QGridLayout **gridlay){
    window2 *cont_one_win=new window2();
    QLabel *label=new QLabel("Test1");
    QVBoxLayout *vblay=new QVBoxLayout;
    vblay->addWidget(label);
    cont_one_win->setLayout(vblay);
    (*gridlay)->addWidget(cont_one_win,0,1);
}

void window1::change_cont_two(QGridLayout **gridlay){
    window2 *cont_one_win=new window2();
    QLabel *label=new QLabel("Test2");
    QVBoxLayout *vblay=new QVBoxLayout;
    vblay->addWidget(label);
    cont_one_win->setLayout(vblay);
    (*gridlay)->addWidget(cont_one_win,0,1);
}


void window1::change_cont_three(QGridLayout **gridlay){
    window2 *cont_one_win=new window2();
    QLabel *label=new QLabel("Test3");
    QVBoxLayout *vblay=new QVBoxLayout;
    vblay->addWidget(label);
    cont_one_win->setLayout(vblay);
    (*gridlay)->addWidget(cont_one_win,0,1);
}
4

1 回答 1

1

您只能连接兼容的信号和插槽。无需通过“兼容性”深入了解细节,您可以考虑类似的参数(按数量和类型匹配)。

在您的情况下,您正在尝试连接两个不兼容的事物。信号:void clicked() 和槽:void scW(QWidget *widget);

如果您想保持原样,您需要 QSignalMapper 将 clicked() 映射到您的方法。像这样的东西:

QSignalMapper * mapper = new QSignalMapper();

connect(mapper, SIGNAL(mapped(QWidget*)), w, SLOT(scW(QWidget*)));

connect(button_eins1,SIGNAL(clicked()),mapper,SLOT(map()));
mapper->setMapping(button_eins1, mainwindow1);

connect(button_zwei1,SIGNAL(clicked()),mapper,SLOT(map()));
mapper->setMapping(button_eins1, mainwindow2);
...
于 2013-04-22T21:29:43.380 回答