본문 바로가기
대외활동/시스템프로그래밍

0808 Qt 메세지박스, 자료구조

by hoshi03 2024. 8. 8.

• QMessageBox

위와 같은 알림 창을 만들기

슬롯을 선언하고 슬롯에서 QMessageBox::question 메서드를 호출해서 어떤 창을 띄울건지를 구현한다

void QtEditor::help(){
    QMessageBox::question(this,"QMessageBox::showQuestion()",
    "Qusetion Message", QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);
}

 

• 파일 입출력

void QtEditor::openFile(){
    QString filename = QFileDialog::getOpenFileName(this, "select file to open", ".", "Text File(*.txt *.c *.cpp *.h)");
    qDebug() << filename;
    if(!filename.length()) return;

    QFileInfo fileInfo(filename);
    if(fileInfo.isReadable()){
        QFile file(filename);
        file.open(QIODevice::ReadOnly | QIODevice::Text);
        QByteArray msg = file.readAll();
        file.close();

        QTextEdit* textedit = newFile();
        textedit->setWindowTitle(filename);
        windowHash.key(textedit)->setText(filename);
        textedit->setPlainText(msg);
    }

    else{
        qDebug() << "contents cannot read";
    }
}

void QtEditor::saveFile(){
    QMdiSubWindow *window = mdiArea->currentSubWindow();
    if (window != nullptr) {
        QTextEdit *textedit = qobject_cast<QTextEdit*>(window->widget());
        QString filename = textedit->windowTitle();
        qDebug() << filename;
        if (filename.isEmpty()) {
            filename = QFileDialog::getSaveFileName(this, "Select a file to save", ".", "Text Files (*.text *.txt *.html *.c *.cpp *.h)");
            if (filename.isEmpty()) return;
            textedit->setWindowTitle(filename);

            // Ensure windowHash contains the QAction associated with the QTextEdit
            QAction *action = windowHash.key(textedit, nullptr);
            if (action) {
                action->setText(filename);
            }
        }

        QFile file(filename);
        if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
            qDebug() << "Cannot open file for writing";
            return;
        }

        // Writing content to file
        QByteArray content = textedit->toHtml().toUtf8();
        if (file.write(content) == -1) {
            qDebug() << "Error writing to file";
        }

        file.close();
    } else {
        qDebug() << "No active subwindow";
    }
}

'대외활동 > 시스템프로그래밍' 카테고리의 다른 글

리눅스 명령어 정리 & 예제  (0) 2024.08.17
0812 리눅스  (0) 2024.08.12
0807 QT 에디터 & 액션  (0) 2024.08.07
0805 QT C++ 다양한 ui, 시그널  (0) 2024.08.05
modern c++  (0) 2024.07.31