0
votes

So I'm a newbie to Qt and I'm trying to create a simple project. 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.

I was searching for some solutions, people were using "Go to slot..." options, which my Visual Studio doesn't provide. So I tried to create my own function.

The MainWindow's name is projekt2, the Dialog I want to open is named addBook.

projekt2.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

#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

#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

#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.

2
Can you please edit the question and post your ui_projekt2.h file?p-a-o-l-o
Remove 'projekt2::' from the declaration of the slot in projekt2.h and see if it fixes the problem.Gabriella Giordano
You have to use QtCreatormarouane18
@GabriellaGiordano oh my god, I just did it and it worked. Thank you so much!P. Paul

2 Answers

0
votes

Qt signal/slot system has several way to perform connections. 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 ();

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.

You can also explicitly connect signals/slots using 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.

0
votes

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