Qt

[Qt] 배경색 변경

_minit 2024. 10. 16. 14:37

Qt Creator로 원도우 창 배경색을 변경해 보자.

배경색을 변경하려면 QPalette라는 헤더 파일이 필요하다.

아래는 배경색 변경 기본 코드이다.

#include <QPalette>

QPalette palette = window.palette();
palette.setColor(QPalette::Window, Qt::lightGray);
window.setPalette(palette);
#include <QApplication>
#include <QWidget>
#include <QLabel>
#include <QPushButton>
#include <QFont>
#include <QObject>
#include <QLineEdit>
#include <QPalette>

void onButtonCilcked(){
    qInfo("Button clicked!");
}

void setFont(QWidget *name, int size){
    QFont font = name->font();
    font.setPointSize(size);
    name->setFont(font);
}

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QWidget window;
    QPalette palette = window.palette();
    palette.setColor(QPalette::Window, Qt::blue);
    window.setPalette(palette);
    window.resize(1280, 720);              // window size
    window.setWindowTitle("Empty window");  // widow name

    QLabel label("Minit blog!", &window);   // text
    label.move(50, 50);                   // text position
    setFont(&label, 20);

    QPushButton button("Button", &window);  // button
    button.move(300, 50);                  // button position
    button.resize(200, 200);                // button size
    setFont(&button, 20);

    QObject::connect(&button, &QPushButton::clicked, &onButtonCilcked); // button click event

    QLineEdit inputfield(&window);
    inputfield.setPlaceholderText("기본 text"); // placeholder text
    inputfield.move(600, 50);             // input text position
    inputfield.resize(200, 200);            // input text size
    setFont(&inputfield, 20);


    window.show();

    return app.exec();
}

blue로 설정하여 눈이 아프지만 배경색이 들어갔다. 그리고 좀 배치가 어지러운 거 같아서 정리도 해주었고 폰트를 설정하는 Qfont 작업이 반복적으로 나오는 걸 보고 함수화도 진행하였다.

728x90

'Qt' 카테고리의 다른 글

[Qt] 현재 시간 들고오기  (0) 2024.10.16
[Qt] qmake란?  (0) 2024.10.16
[Qt] 텍스트 입력란  (0) 2024.10.16
[Qt] 버튼 클릭 이벤트  (1) 2024.10.16
[Qt] 버튼 만들기  (0) 2024.10.16