【问题标题】:Qt call Child-Window on a button clickQt 在单击按钮时调用 Child-Window
【发布时间】:2017-04-13 13:26:52
【问题描述】:

我是 Qt 的新手。我正在尝试从 myMyMainWindow 打开花药 window 。我无法理解,我在这种情况下做错了什么。不想让你们解决我的问题,只是说请,我做错了什么。

所以我有一个MainWindow.h(看看这个评论,认为你不需要了解它的整个过程):

#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

我的MyMainWindow.cpp 文件:

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

所以,我想我应该给你看第二个窗口,可能有错误:

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

还有.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);
}

总的来说,我的 MainWindow 出现在屏幕上,单击按钮 timer 后,我没有任何操作。请帮助我,如果可以的话。谢谢。

【问题讨论】:

    标签: c++ qt


    【解决方案1】:

    这是您的代码中的错字。您需要阅读应用程序的输出以了解原因。

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

    我建议你使用Qt5 syntax

    【讨论】:

    • 试过了,还是一样,还是不行。我在MyMainWindow 中创建T_timer *myTimer; 可能会出错吗?
    • 请更新您的代码,因为它不可编译。顺便说一句,尝试使用myTimer-&gt;exec() 而不是show()。并且不要忘记释放内存。不管怎样,你有很多脏代码,试着回顾一下 Qt 示例。
    • 已更改。但是没有成功。可以给个链接吗? dropbox.com/sh/g4b6kd72fcncm5h/AACRDfO7S3QZbMFpyT_833Cpa?dl=0
    • 你没有修复connect(timer_Button,SIGNAL(click()),...。做我在回答中提出的建议。
    【解决方案2】:

    我不知道你为什么在这种情况下使用 QLayout,我建议你看看这篇文章:here

    但要解决您的问题,请尝试将您的代码更改为:


    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:将对话框更改为您的窗口。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-15
      • 2018-07-04
      • 1970-01-01
      • 1970-01-01
      • 2019-06-04
      • 1970-01-01
      相关资源
      最近更新 更多