반응형

 

○ 필요 헤더

#include <QHostAddress>
#include <QNetworkInterface>

○ 네트워크 인터페이스 전체 주소 가져오기

QList<QHostAddress> addrList = QNetworkInterface::allAddresses();

○ 네트워크 인터페이스 전체 주소 중 IPv4 주소만 출력하기

#include <QHostAddress>
#include <QNetworkInterface>
#include <QDebug>	// 디버그 출력용

QList<QHostAddress> addrList = QNetworkInterface::allAddresses();

foreach(QHostAddress addr, addrList)
{
    if (0 < addr.toIPv4Address())
    {
        qDebug() << addr.toString();
    }
}

- 실행 결과

"192.168.56.1"
"127.0.0.1"

- IPv4, IPv6 인지 확인

// IPv4
quint32 QHostAddress::toIPv4Address(bool *ok) const

// IPv6
Q_IPV6ADDR QHostAddress::toIPv6Address() const
반응형
,
반응형

 

QFile를 이용하여 간단하게 파일 존재 여부, 복사, 삭제 등을 할 수 있어요.

 

○ 헤더

#include <QFile>

○ 파일 존재 여부 확인

bool QFile::exists() const

bool result = QFile::exists(file);

○ 파일 복사

bool QFile::copy(const QString &newName)

bool result = QFile::copy(file, copy_file);

○ 파일 삭제

[static] bool QFile::remove(const QString &fileName)

bool result = QFile::remove(FilePath);

○ 사용 예제 (파일 확인 후 복사, 삭제)

#include <QFile>

QString file = "/home/ubuntu/test.mp4";
QString copy_file = "/home/ubuntu/test_copy.mp4";

if (QFile::exists(file) == true)
{
    if (QFile::copy(file, copy_file) == true)
    {
        QFile::remove(copy_file);
    }
}
반응형
,
반응형

 

QList안에 있는 구조체 아이템의 항목을 기준으로 아이템을 정렬할 수 있어요.

 

○ 헤더

#include <QDebug>	// qDebug() 출력을 위해 추가

struct People
{
    QString Name;
    int Age;
};

 void ShowList(QList<People> list);	// QList 내용 출력 용도

 

○ 메인

void MainWindow::ShowList(QList<People> list)
{
    foreach (People people, list)
    {
        qDebug() << QString("Name : %1 / Age : %2").arg(people.Name).arg(people.Age);
    }
}

// 이름 정렬
bool NameLessThan(const People &s1, const People &s2)
{
    return s1.Name.toLower() < s2.Name.toLower();
}

// 나이 정렬
bool AgeLessThan(const People &s1, const People &s2)
{
    return s1.Age < s2.Age;
}

// Main
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    // 사람 정보 추가
    QList<People> peopleList;
    peopleList.append(People {"Ghi", 10});
    peopleList.append(People {"Def", 30});
    peopleList.append(People {"Abc", 20});

    qDebug("People List");
    ShowList(peopleList);

    // 이름 정렬
    std::sort(peopleList.begin(), peopleList.end(), NameLessThan);
    qDebug("Sort - Name");
    ShowList(peopleList);

    // 나이 정렬
    std::sort(peopleList.begin(), peopleList.end(), AgeLessThan);
    qDebug("Sort - Age");
    ShowList(peopleList);
}

 

○ 실행 결과

Debugging starts
People List
"Name : Ghi / Age : 10"
"Name : Def / Age : 30"
"Name : Abc / Age : 20"
Sort - Name
"Name : Abc / Age : 20"
"Name : Def / Age : 30"
"Name : Ghi / Age : 10"
Sort - Age
"Name : Ghi / Age : 10"
"Name : Abc / Age : 20"
"Name : Def / Age : 30"
반응형
,
반응형

 

○ 헤더 및 함수 사용 방법

Header:
#include <QMessageBox> 

qmake:
QT += widgets

QMessageBox(QWidget *parent = Q_NULLPTR)
QMessageBox(Icon icon, const QString &title, const QString &text, StandardButtons buttons = NoButton, QWidget *parent = Q_NULLPTR, Qt::WindowFlags f = Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint)

○ 일반 메시지 박스

#include <QMessageBox> 

QMessageBox msgBox;
msgBox.setText("setText");
msgBox.exec();


○ 메시지 박스 아이콘 표시

#include <QMessageBox> 

QMessageBox::question(this, "Title", "Message");
QMessageBox::information(this, "Title", "Message");
QMessageBox::warning(this, "Title", "Message");
QMessageBox::critical(this, "Title", "Message");


○ 선택형 메시지 박스 출력

#include <QMessageBox>
#include <QDebug>

QMessageBox msgBox;
msgBox.setText("setText");
msgBox.setInformativeText("setInformativeText");
msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Ok);
int ret = msgBox.exec();

switch (ret)
{
case QMessageBox::Ok:
    qDebug() << "QMessageBox::Ok";
    break;

case QMessageBox::Cancel:
    qDebug() << "QMessageBox::Cancel";
    break;

default:
    qDebug() << "default";
    break;
}

 

- 버튼 유형

QMessageBox::Ok
QMessageBox::Open
QMessageBox::Save
QMessageBox::Cancel
QMessageBox::Close
QMessageBox::Discard
QMessageBox::Apply
QMessageBox::Reset
QMessageBox::RestoreDefaults
QMessageBox::Help
QMessageBox::SaveAll
QMessageBox::Yes
QMessageBox::YesToAll
QMessageBox::No
QMessageBox::NoToAll
QMessageBox::Abort
QMessageBox::Retry
QMessageBox::Ignore
QMessageBox::NoButton

 

- 메시지 박스 아이콘 형에 버튼을 추가하여 사용자가 선택한 버튼의 결과를 알아낼 수 있어요.

int ret = QMessageBox::warning(this, "Title", "Msg.", QMessageBox::Ok | QMessageBox::Cancel, QMessageBox::Ok);

 

반응형
,
반응형

 

access()를 이용하여 해당하는 파일이 있나, 읽기, 쓰기 실행을 할 수 있는지 알 수 있어요.

 

○ 헤더 및 함수

#include <unistd.h>

// *pathname : 파일 경로
// mode : 파일에 대한 확인 모드 (F_OK, R_OK, W_OK, X_OK)
int access(const char *pathname, int mode);

○ 리턴값 

0 : 성공 (파일 있음, 접근 가능)

-1 : 실패 (파일 없음, 접근 권한 없음)

 

○ 예제

#include <unistd.h>

const char* file = "/home/ubuntu/test.mp4";

if (access(file, F_OK) == 0)
{
}

if (access(file, R_OK) == 0)
{
}

 

 

반응형
,