Qt5 creating a Qaction that calls an information window

Asked

Viewed 860 times

1

I am developing a solution in Qt5.4.2, in my menu bar, when clicking an option, a new window should open with that information.

I was thinking about doing one QDialog, but depending on it would get complicated to edit some things and put images, for example. So I created ui'which will be called by these actions triggered by the signal triggered.

But I can’t get the window call to open when the signal is triggered.

I imported the library .h of the window I want to call, in case the aboutMain. I created an object of hers (a new window) and now I want to show.

That’s the way it is?

brprint. h -> . h main

#include <QMainWindow>
#include "loading.h"

namespace Ui {
class BrPrint3D;
}

class BrPrint3D : public QMainWindow
{
    Q_OBJECT

public:
    explicit BrPrint3D(QWidget *parent = 0);
    ~BrPrint3D();
    void init();

private slots:    
    void on_actionSobre_BrPrint3D_triggered();

private:
    Ui::BrPrint3D *ui;

};

brprint3d.cpp

#include "brprint3d.h"
#include "ui_Pandora.h"
#include "aboutbrprint.h"
#include <stdio.h>

//Sobre->Sobre o BrPrint3D
void BrPrint3D::on_actionSobre_BrPrint3D_triggered()
{
    //Chamar Janela Sobre o BrPrint
    aboutBrPrint window;
    window.show();


}

aboutBrPrint. h

#include <QWidget>

namespace Ui {
class aboutBrPrint;
}

class aboutBrPrint : public QWidget
{
    Q_OBJECT

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

private:
    Ui::aboutBrPrint *ui;
};

Main

#include "brprint3d.h"
#include <QApplication>
#include "loading.h"
#include <QTranslator>

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

    BrPrint3D w;
    w.show();
    w.init();
    return app.exec();
}
  • So, and a menu bar action, so when I create an item there it already creates a direct action, I only define after which is the signal. In this case I chose Triggered, which is the most click-like action there is. When I build, other features work, but when I click on this item, nothing happens. I right-clicked on the item, Go To Slot, Triggered action. It is declared as Private Slot.

  • Edited.

  • This form of statement is deprecated in Qt 5.2. it is in that direct form that I actually made...

  • Sorry Lays, I use Qt since version 5.0 and did not know this, could you provide me the source of such information? As far as I know these "writing/running styles" are C++ and not QT and maybe it changes according to the C++ version and not QT, for example from c++89 to C++11 there were changes. I would be grateful if you could provide me with the link of such information. Thank you.

  • Well I based myself on the main generated by Qt itself. For there it shows how it calls the Main window for example. I will put the example of code up there.

  • I cannot do Signal in hand, because the pandora.ui does not recognize the object aboutBrPrint. And this Signal is generated automatically when I create the item in the menu bar. I can’t get another one to over-write it, otherwise I would have solved the case, because I did it with another question of windows. I cannot create a separate connect. = / Pq in this case the action is triggered by a menu bar option, not a push buttom.

Show 1 more comment

1 answer

1


I believe it is necessary to use the new, because the first generated window is already linked to QApplication, the new windows must receive the this to be linked to each other, then the right thing would be this:

void BrPrint3D::on_actionSobre_BrPrint3D_triggered()
{
    aboutBrPrint *window = new aboutBrPrint(this);
    window->show();
}

In the case exec may also work (note that this will probably block the "parent" window while open), as the exec can reference the "parent" window, to unlock the parent window just close the aboutBrPrint:

void BrPrint3D::on_actionSobre_BrPrint3D_triggered()
{
    aboutBrPrint window;
    window.exec();
}

In main.cpp we do not use the constructor new because the first window is already linked to QOBJECT (believe me) that gets connected to the application, then the new is not in "disuse", enjoy and read this link When to choose whether to use a pointer when creating an object?

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

    BrPrint3D w;
    w.show();
    w.init();
    return app.exec();
}
  • That your first solution worked, but instead of opening in the separate window, she opened the text inside the parent window.

  • @Laysrodrigues "inside" as if you had used "addChild" or just superimposed on "parent window"? Send an example of the zip problem if you can.

  • I’ve already solved it with the following solution: void Brprint3d::on_actionSobre_o_BrPrint3D_triggered() ! Qwidget *x; aboutBrPrint *w= new aboutBrPrint(x); w->show(); }

  • Haha then dig further as I am in that situation: If it works-> next item. Thank you so much for the help. ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.