Problem in displaying window?



  • If i am creating a window inside another window class constructor, then why the window is not visible while i am using function window.show().



  • Could you be a bit more elaborate? How are you construting the "windows", and what makes you think something will be shown just because you are creating it in a constructor?

    If I understand you correctly, you have two widgets a (of class A) and b (of class B), and you create b inside the constructor of A? Then you call a.show(). And you excpect to be able to see b inside a?

    This should normally be the case, provided that you also made a b child of a, e.g. by passing a as the parent argument of B's constructor when you create b.



  • You could have been a little more specific. Whether you are using Dialogs or Mainwindow or whatever.. Or your code.

    [oops, not timely posted.. Bit late ;)]



  • ya i am creating inside the constructor using QMainwindow class.



  • A QMainWindow has a central widget, which you set using the setCentralWidget() function, and dock windows, which you can add using addDockWidget(). Did you use one of those functions to add your widget to the main window?



  • You will have to provide some sample code (at least your ctor). Everything else is just groping in the dark.



  • This is my code please check it what is the problem
    @#include <QtGui>
    #include <QObject>
    #include "mainwindow.h"
    #include "window1.h"

    class MyWindow: public QWidget
    {

    Q_OBJECT
    

    public:
    MyWindow(QWidget *parent = 0):QWidget(parent)
    {
    qDebug ()<<"window constructor";
    float button_width = 47;
    float button_height = 47;
    int button_states = 1;

        QMainWindow new_window;
        QPixmap pixmap(":/endcall.png");
        Button *button1  = new Button(button_width, button_height, button_states, pixmap, this);
        button1->setGeometry(60, 70, 80, 90);
    
        QObject::connect(button1, SIGNAL(pressed()), &new_window, SLOT(show()));
        //QObject::connect(button1, SIGNAL(pressed()), button1,     SLOT(press()));
    
    
        pixmap.load(":/und_speaker.png");
        button_width  = 17;
        button_height = 14;
    
        Button *button2  = new Button(button_width, button_height, 1, pixmap, this);
        button2->setGeometry(100,110,120,130);
    
        pixmap.load(":/audio.png");
        button_width = 17;
        button_height = 16;
    
        Button *button3  = new Button(button_width, button_height, 2, pixmap, this);
        button3->setGeometry(130,140,150,160);
    
    }
    
    ~ MyWindow(){}
    
    
    void mousePressEvent(QMouseEvent *k)
    {
         qDebug ()<<"window mouse press event";
    
         QWidget::mousePressEvent(k);
    }
    
    void mouseReleaseEvent(QMouseEvent *h)
    {
        qDebug ()<<"window mouse release event";
        QWidget::mouseReleaseEvent(h);
    }
    

    };@

    in the above code "new_window" is not opening while pressing the button.



  • new_window is created on the stack, not on the heap. As soon as the ctor is left new_window will be automatically destroyed (and thus not shown). new_window will have to be created on the heap.

    @
    class MyWindow : public QWidget
    {
    ...
    MyWindow(QWidget* parent = 0) : QWidget(parent)
    {
    ...
    mainWindow = new QMainWindow(this);
    ...
    }
    ...
    private:
    QMainWindow* mainWindow;
    }
    @



  • thank's for the suggestion. In this program i want to notify button when window is closed how can i do it?



  • Emit a signal in the QMainWindow::closeEvent() (by subclassing or using an event filter), which is connected to a slot in your MyWindow class. Use this slot to modify the button.



  • can you show that by writing an example



  • @
    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    MainWindow(QWidget* parent = 0, Qt::WindowFlags flags = 0) : QMainWindow(parent, flags) {}

    protected:
    void closeEvent(QCloseEvent* event)
    {
    emit closed();
    QMainWindow::closeEvent(event);
    }

    signals:
    void closed();
    };

    class MyWindow : public QWidget
    {
    Q_OBJECT

    public:
    MyWindow(QWidget* parent = 0) : QWidget(parent)
    {
    ...
    connect(mainWindow, SIGNAL(closed()), this, SLOT(updateButton()));
    }

    public slots:
    void updateButton()
    {
    button->...
    }

    private:
    MainWindow* mainWindow;
    };
    @
    Brain to terminal. Not tested. Exemplary.



  • thanks it is working. can we control mouse press and release event also like this?



  • You can handle any event listed "here":http://doc.qt.nokia.com/latest/qwidget.html#protected-functions.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.