[C++|QT] Zrzut ekranu
Ten wpis jest o tworzeniu zrzutu ekranu za pomocą biblioteki QT.
Zacznijmy od utworzenia Qt Widgets Application w Qt Creator.
main.cpp
#include <QApplication>
#include <QScreen>
#include <QPixmap>
#include <QDateTime>
#include <QPushButton>
#include <QVBoxLayout>
#include <QWidget>
#include <QMessageBox>
class ScreenshotWidget : public QWidget {
Q_OBJECT
public:
ScreenshotWidget() {
// Ustawiamy tytuł okna dla programu
setWindowTitle("Zrób zrzut ekranu");
// Tworzymy przycisk
QPushButton *button = new QPushButton("Wykonaj zrzut", this);
// Ustawiamy układ
QVBoxLayout *layout = new QVBoxLayout(this);
layout->addWidget(button);
setLayout(layout);
// Połączenie sygnału kliknięcia przycisku z funkcją takeScreenshot
connect(button, &QPushButton::clicked, this, &ScreenshotWidget::takeScreenshot);
}
private slots:
void takeScreenshot() {
// Uzyskajemy dostęp do ekranu
QScreen *screen = QGuiApplication::primaryScreen();
if(screen) {
// Robimy zrzut ekranu
QPixmap screenshot = screen->grabWindow(0);
// Ustalamy nazwę pliku na podstawie aktualnego czasu
QString timestamp = QDateTime::currentDateTime().toString("yyyy-MM-dd_HH-mm");
QString fileName = QString("screenshot_%1.png").arg(timestamp);
// Zapisujemy zrzut ekranu
screenshot.save(fileName, "PNG");
// Wyświetlamy komunikat informujący o zapisaniu zrzutu
QMessageBox::information(this, "Zrzut ekranu", "Zrzut ekranu został zapisany jako:\n" + fileName);
}
}
};
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
ScreenshotWidget w;
w.resize(300, 100); // Ustawiamy rozmiar okna programu
w.show();
return a.exec();
}
#include "main.moc"
Komentarze
Prześlij komentarz
Dzięki za komentarz!