๋ฐ˜์‘ํ˜•

์†Œ๊ฐœ

๋ณดํ†ต ์ƒˆ๋กœ์šด ์–ธ์–ด๋‚˜ ์ปดํŒŒ์ผ๋Ÿฌ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ฒซ ์˜ˆ์ œ ํ”„๋กœ์ ํŠธ๊ฐ€ 'Hello World'๋กœ ์‹œ์ž‘ํ•˜์ฃ ?

์ € ๊ฐ™์€ ๊ฒฝ์šฐ๋Š” Hello World ๋‹ค์Œ์œผ๋กœ ์ด๋ฏธ์ง€ ํŒŒ์ผ ์„ ํƒ ํ›„ ๊ทธ๋ฆฌ๋Š” ์ž‘์—…์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.

์ด๋ฏธ์ง€ ๊ฐ์ฒด๋Š” ์–ด๋–ค ๊ฒƒ์ด ์žˆ๋Š”์ง€, ๊ทธ๋ ค์ง€๋Š” ๋Œ€์ƒ์—๋Š” ์–ด๋–ค ์ปจํŠธ๋กค์ด ์‚ฌ์šฉ๋˜๋Š”์ง€๋ฅผ ํ™•์ธํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€์žฅ ๊ธฐ๋ณธ ๋‹จ๊ณ„์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

๊ทธ๋ž˜์„œ ์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” Button์„ ํด๋ฆญํ•˜๋ฉด ์ด๋ฏธ์ง€ ํŒŒ์ผ์„ ์„ ํƒํ•˜๊ณ , ์„ ํƒ๋œ ์ด๋ฏธ์ง€๋ฅผ Label์— ๊ทธ๋ฆฌ๋Š” ์˜ˆ์ œ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.


์ด๋ฒˆ ํ”„๋กœ์ ํŠธ๋ฅผ ์‹œ์ž‘ํ•˜๊ธฐ ์ „์—, ์•„๋ž˜ ๋งํฌ๋ฅผ ์ฐธ๊ณ ํ•˜์‹œ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.


๋”ฐ๋ผํ•˜๊ธฐ

์ด๋ฒˆ ํ”„๋กœ์ ํŠธ๋Š” QDialog ๊ธฐ๋ฐ˜์œผ๋กœ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  Qt Designer์—์„œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด Push Button๊ณผ QLabel์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

์ €๋Š” ์ด๋ฏธ์ง€ ์ •๋ณด๋ฅผ ํ‘œ์‹œํ•˜๊ธฐ ์œ„ํ•ด ์—ฌ๋Ÿฌ ๊ฐœ์˜ QLabel์„ ๋ณ„๋„๋กœ ์ถ”๊ฐ€ํ–ˆ์Šต๋‹ˆ๋‹ค.

Qt Designer ๋ฐฐ์น˜ ํ™”๋ฉด


Visual Studio์—์„œ ํ—ค๋” ํŒŒ์ผ์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด Slot ํ•จ์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. 

#pragma once

#include <QtWidgets/QDialog>
#include "ui_QtGuiApplication1.h"
#include <qfiledialog.h>

class QtGuiApplication1 : public QDialog
{
	Q_OBJECT

public:
	QtGuiApplication1(QWidget *parent = Q_NULLPTR);

private:
	Ui::QtGuiApplication1Class ui;

private slots:
	void ButtonClicked();
};


์†Œ์Šค ํŒŒ์ผ์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

#include "QtGuiApplication1.h"

QtGuiApplication1::QtGuiApplication1(QWidget *parent)
	: QDialog(parent)
{
	ui.setupUi(this);

	connect(ui.pushButton_LoadImage, SIGNAL(clicked()), this, SLOT(ButtonClicked()));
}

void QtGuiApplication1::ButtonClicked()
{
	QFileDialog dlg;
	QString filePath = dlg.getOpenFileName(this, "Load Image", "", "Image Files (*.png *.jpg *.bmp)");
	
	QString fileName = filePath.section("/", -1);
	ui.label_FileName->setText(fileName);
	
	QPixmap img;
	img.load(filePath);

	int w = img.width();
	ui.label_Width->setText(QString::number(w));
	int h = img.height();
	ui.label_Height->setText(QString::number(h));
	
	ui.label_Display->setPixmap(img);
	ui.label_Display->setScaledContents(true);
}


17๋ฒˆ์งธ ์ค„์€ ํŒŒ์ผ ์ด๋ฆ„์„ ์ถ”์ถœํ•˜๊ธฐ ์œ„ํ•ด section ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.

28๋ฒˆ์งธ ์ค„์˜ setPixmap ํ•จ์ˆ˜๋Š” ๋ถˆ๋Ÿฌ์˜จ QPixmap ์ด๋ฏธ์ง€ ์ •๋ณด๋ฅผ QLabel์— ์„ค์ •ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

29๋ฒˆ์งธ ์ค„์˜ setScaledContents ํ•จ์ˆ˜๋Š” QLabel์˜ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๊ณต๊ฐ„์„ ์ฑ„์šฐ๋Š” ๋™์ž‘ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

์ฆ‰, ์„ค์ •๋œ Pixmap์˜ ํฌ๊ธฐ๊ฐ€ ์กฐ์ •๋ฉ๋‹ˆ๋‹ค.


๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

ํ”„๋กœ์ ํŠธ ์˜ˆ์ œ ๊ฒฐ๊ณผ

Image Drawing Example.zip

๋ฐ˜์‘ํ˜•