0

I am new in Qt. And I am trying to open anther window from myMyMainWindow . I can't catch, what I am doing wrong with this situation. Don't want you guys to solve my problem, just say to please, what I am doing wrong.

So I have got a MainWindow.h( look at this comment, think you don't need to understand the whole proccess of it):

#ifndef MYMAINWINDOW_H
#define MYMAINWINDOW_H

#include <QDialog>
#include <QMainWindow>
#include <QPushButton>
#include <QLayout>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include "timer.h"

class MyMainWindow: public QMainWindow
{
    Q_OBJECT
private:
    QPushButton *timer_Button;
    QPushButton *StopWatch;
    QPushButton *Close;
    T_timer *myTimer;
public:
    MyMainWindow(QWidget *parent);
public slots:
    void Open_Timer_Window(); // Slot for opening a new window
};

#endif // MYMAINWINDOW_H

My MyMainWindow.cpp file:

#include "MyMainWindow.h"

MyMainWindow::MyMainWindow(QWidget *parent=0): QDialog(parent)
{
    // just creating Buttons
    timer_Button = new QPushButton ("Timer");

    Close=new QPushButton("Close");

    QHBoxLayout *Up=new QHBoxLayout;
    Up->addWidget(timer_Button);
    QHBoxLayout *Down=new QHBoxLayout;
    Down->addWidget(Close);
    QVBoxLayout *Main=new QVBoxLayout;
    Main->addLayout(Up);
    Main->addLayout(Down);

    // the main part 
    connect(Close,SIGNAL(clicked()),this,SLOT(close()));
    connect(timer_Button,SIGNAL(clicked()),this,SLOT(Open_Timer_Window()));// call `Slot of Open_Timer_Window()`

    setLayout(Main);
    setWindowTitle("Smart Watch");

}

void MyMainWindow::Open_Timer_Window()
{
     myTimer = new T_timer(0);
     myTimer->show();
}

So, I think I should show you the Second window, might be there is a mistake:

The header:

#include <QPushButton>
#include <QDialog>
#include <QHBoxLayout>
#include <QVBoxLayout>

class T_timer : public QDialog
{
    Q_OBJECT
private:
    QPushButton Start;
    QPushButton Stop;

public:
    T_timer(QWidget *parent=0);
};

And .cpp:

 #include "timer.h"

T_timer::T_timer(QWidget *parent=0): QDialog(parent)
{
    Start=new QPushButton ("Start");
    Stop=new QPushButton ("Stop");

    QHBoxLayout *Up=new QHBoxLayout;
    Up->addWidget(Start);
    Up->addWidget(Stop);

    setLayout(Up);
}

Totally, I have got my MainWindow on the screen and after click a button timer , I haven't got an action.Help me please, if you can. Thanks.

Nick
  • 7
  • 6

2 Answers2

0

It's a typo in your code. You need to read an output from your application to understand reasons.

connect(timer_Button,SIGNAL(click()clicked()),this,SLOT(Open_Timer_Window()));

I propose you to use Qt5 syntax.

Dmitry Sazonov
  • 8,801
  • 1
  • 35
  • 61
  • Tried this to do and all the same It doesn't work. May I be mistaken in creating `T_timer *myTimer;` in `MyMainWindow`? – Nick Apr 13 '17 at 13:44
  • Please, update your code, because it is not compilable. Btw, try to use `myTimer->exec()` instead of `show()`. And don't forget to release memory. Anyway, you have a lot of dirty code, try to review Qt examples. – Dmitry Sazonov Apr 13 '17 at 14:33
  • Changed. But there is no success.Can I give a link? https://www.dropbox.com/sh/g4b6kd72fcncm5h/AACRDfO7S3QZbMFpyT_833Cpa?dl=0 – Nick Apr 13 '17 at 14:38
  • You didn't fix `connect(timer_Button,SIGNAL(click()),...`. Do what I proposed in my answer. – Dmitry Sazonov Apr 13 '17 at 14:39
0

I don´t know why you're using QLayout in this case, I'd advise taking a look at this post: here

But to solve you problem try change you code to this:


connect(Close, SIGNAL(clicked(bool)), this, SLOT(close()));
connect(timer_Button, SIGNAL(clicked(bool)),this,SLOT(Open_Timer_Window()));

void MainWindow::Open_Timer_Window() {
    Dialog dlg;
    dlg.setModal(true);
    dlg.show();
    dlg.exec();
}

OBS: Change Dialog to your window.

Community
  • 1
  • 1
Jhonny Pinheiro
  • 308
  • 3
  • 19