前回はハローワールドと表示することをやりしたが、今回は別ウィンドウを作り、表示させてみようと思います。全体のプログラムは以下のようにしてみました。
ソースファイルは下記に示します。
URL:https://drive.google.com/file/d/0B0oSFZs-dbRZVzFxNEVYWFlYZDA/edit?usp=sharing
widget.h:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QMdiSubWindow>
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
QPushButton *m_button1;
QPushButton *m_button2;
QPushButton *s_button1;
QLabel *label1;
void m_button1_clicked();
QWidget *SubW;
QVBoxLayout *vlayout;
QHBoxLayout *hlayout;
private slots:
void openwindow();
};
#endif // WIDGET_H
widget.cpp:
#include "widget.h"
#include "QApplication"
#include <QWidget>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
hlayout=new QHBoxLayout(this);
m_button1=new QPushButton(this);
m_button1->setText(QApplication::translate("Open","開く",0,QApplication::UnicodeUTF8));
m_button2=new QPushButton(this);
m_button2->setText(QApplication::translate("Close","閉じる",0,QApplication::UnicodeUTF8));
hlayout->addWidget(m_button1,0);
hlayout->addWidget(m_button2,1);
this->setLayout(hlayout);
connect(m_button1,SIGNAL(clicked()),this,SLOT(openwindow()));
connect(m_button2,SIGNAL(clicked()),this,SLOT(close()));
}
Widget::~Widget()
{
}
void Widget::openwindow()
{
SubW = new QWidget;
label1=new QLabel(SubW);
label1->setText(QApplication::translate("HelloWorld","ハローワールド",0,QApplication::UnicodeUTF8));
s_button1=new QPushButton(SubW);
s_button1->setText(QApplication::translate("widget","閉じる",0,QApplication::UnicodeUTF8));
vlayout=new QVBoxLayout(SubW);
vlayout->addWidget(label1,0);
vlayout->addWidget(s_button1,1);
SubW->setLayout(vlayout);
connect(s_button1,SIGNAL(clicked()),SubW,SLOT(close()));
SubW->show();
}
動作させると以下の画像のようになりました。
まずは成功です
ソースファイルは下記に示します。
URL:https://drive.google.com/file/d/0B0oSFZs-dbRZVzFxNEVYWFlYZDA/edit?usp=sharing
widget.h:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QMdiSubWindow>
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
QPushButton *m_button1;
QPushButton *m_button2;
QPushButton *s_button1;
QLabel *label1;
void m_button1_clicked();
QWidget *SubW;
QVBoxLayout *vlayout;
QHBoxLayout *hlayout;
private slots:
void openwindow();
};
#endif // WIDGET_H
widget.cpp:
#include "widget.h"
#include "QApplication"
#include <QWidget>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
hlayout=new QHBoxLayout(this);
m_button1=new QPushButton(this);
m_button1->setText(QApplication::translate("Open","開く",0,QApplication::UnicodeUTF8));
m_button2=new QPushButton(this);
m_button2->setText(QApplication::translate("Close","閉じる",0,QApplication::UnicodeUTF8));
hlayout->addWidget(m_button1,0);
hlayout->addWidget(m_button2,1);
this->setLayout(hlayout);
connect(m_button1,SIGNAL(clicked()),this,SLOT(openwindow()));
connect(m_button2,SIGNAL(clicked()),this,SLOT(close()));
}
Widget::~Widget()
{
}
void Widget::openwindow()
{
SubW = new QWidget;
label1=new QLabel(SubW);
label1->setText(QApplication::translate("HelloWorld","ハローワールド",0,QApplication::UnicodeUTF8));
s_button1=new QPushButton(SubW);
s_button1->setText(QApplication::translate("widget","閉じる",0,QApplication::UnicodeUTF8));
vlayout=new QVBoxLayout(SubW);
vlayout->addWidget(label1,0);
vlayout->addWidget(s_button1,1);
SubW->setLayout(vlayout);
connect(s_button1,SIGNAL(clicked()),SubW,SLOT(close()));
SubW->show();
}
動作させると以下の画像のようになりました。
まずは成功です

コメント
コメントを投稿