简体   繁体   English

Qt 从主窗口打开对话框

[英]Qt open dialog from main window

So I'm a newbie to Qt and I'm trying to create a simple project.所以我是 Qt 的新手,我正在尝试创建一个简单的项目。 I've got a MainWindow with some buttons and stuff that I created using the Qt Designer, and once I click one of these buttons (it's name is newBook), I need to open another dialog.我有一个带有一些按钮和我使用 Qt 设计器创建的东西的 MainWindow,一旦我单击其中一个按钮(它的名称是 newBook),我需要打开另一个对话框。

I was searching for some solutions, people were using "Go to slot..." options, which my Visual Studio doesn't provide.我正在寻找一些解决方案,人们正在使用我的 Visual Studio 不提供的“转到插槽...”选项。 So I tried to create my own function.所以我尝试创建自己的函数。

The MainWindow's name is projekt2, the Dialog I want to open is named addBook. MainWindow 的名称是 projekt2,我要打开的 Dialog 名为 addBook。

projekt2.h项目2.h

#pragma once

#include <QtWidgets/QMainWindow>
#include "ui_projekt2.h"

class projekt2 : public QMainWindow
{
    Q_OBJECT

public:
    projekt2(QWidget *parent = Q_NULLPTR);


private:
    Ui::projekt2Class ui;

protected slots:
    void projekt2::on_newBook_clicked();
};

projekt2.cpp项目2.cpp

#include "stdafx.h"
#include "projekt2.h"
#include "addbook.h"

projekt2::projekt2(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);
}

void projekt2::on_newBook_clicked()
{
    addBook book(this);
    book.setModal(true);
    book.exec();
}

addbook.h addbook.h

#pragma once

#include <QDialog>
#include "ui_addbook.h"

class addBook : public QDialog
{
    Q_OBJECT

public:
    addBook(QWidget *parent = Q_NULLPTR);
    ~addBook();

private:
    Ui::addBook ui;
};

addbook.cpp addbook.cpp

#include "stdafx.h"
#include "addbook.h"

addBook::addBook(QWidget *parent)
    : QDialog(parent)
{
    ui.setupUi(this);
}

addBook::~addBook()
{
}

When i run this, there are no errors.当我运行它时,没有错误。 My MainWindow opens, but when I click the button, nothing happens.我的主窗口打开,但是当我单击按钮时,什么也没有发生。 I'm sure I'm missing something, like a connection, but I can't figure it out.我确定我错过了一些东西,比如连接,但我无法弄清楚。

I apologize for a trivial question, but I'm a bit frustrated now.我为一个微不足道的问题道歉,但我现在有点沮丧。 Thanks for your patience.谢谢你的耐心。

Qt signal/slot system has several way to perform connections. Qt信号/插槽系统有几种执行连接的方法。 One of these is the automatic connection based on matching the name of the widget and the signal when a slot follows this naming convenction: "on_" + widgetName + "_" + signalName (); 其中之一是在插槽遵循此命名约定时基于控件名称和信号匹配的自动连接:“ on_” + widgetName +“ _” + signalName();

But for this to work you have to feed the Qt metacompiler with well-formed header files, so remove the 'projekt2::' prefix from the declaration of the on_newButton_clicked() slot. 但是,要执行此操作,您必须为Qt元编译器提供格式正确的头文件,因此请从on_newButton_clicked()插槽的声明中删除“ projekt2 ::”前缀。

You can also explicitly connect signals/slots using Qt connect() ; 您还可以使用Qt connect()显式连接信号/插槽; check this link to learn more about it. 检查此链接以了解更多信息。 connect() should be your default way of doing this, as automatic connection based on name matching can easily be broken renaming a widget, and the Qt metacompiler or the compiler itself won't be complaining about it. connect()应该是您执行此操作的默认方式,因为基于名称匹配的自动连接很容易被重命名为小部件,并且Qt元编译器或编译器本身不会对此有所抱怨。

Try this: 尝试这个:

#include "stdafx.h"
#include "projekt2.h"
#include "addbook.h"

projekt2::projekt2(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);
    connect(ui.newBook,SIGNAL(clicked(),this,SLOT(on_newBook_clicked()));
}

void projekt2::on_newBook_clicked()
{
    addBook *book;
    book = new addBook(this)
    book->setModal(true);
    book->show();
}

Try this, this worked for me试试这个,这对我有用

    void projekt2::on_newBook_clicked()
    {
        addBook book;
        book.setModal(true);
        book.exec();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM