Why its not saving any file?
#include "mainwindow.h"
#include <QApplication>
#include <QPixmap>
#include <QPainter>
#include <QList>
#include <QScreen>
QPixmap grabScreens() {
auto screens = QGuiApplication::screens();
QList<QPixmap> scrs;
int w = 0, h = 0, p = 0;
foreach (auto scr, screens) {
QPixmap pix = scr->grabWindow(0);
w += pix.width();
if (h < pix.height()) h = pix.height();
scrs << pix;
}
QPixmap final(w, h);
QPainter painter(&final);
final.fill(Qt::black);
foreach (auto scr, scrs) {
painter.drawPixmap(QPoint(p, 0), scr);
p += scr.width();
}
return final;
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QPixmap pixmap = grabScreens();
QFile file("file.jpg");
file.open(QIODevice::WriteOnly);
pixmap.save(&file, "JPG", 1);
MainWindow w;
w.show();
return a.exec();
}
The file you're looking for should be in the same folder of the executable.
If you're running your code from Qtcreator, it should be in the build directory, as specified in the Build Settings of the Projects page.
You should consider using QStandardPaths to query for a writable location for the screenshot to be saved to. This will avoid the issue of trying to write to a read-only directory.
Related
On Linux I can use this pipeline QMimeDatabase::mimeTypeForFile > QMimeType::iconName > QIcon::fromTheme to get icons for files. Afaik the latter works on Linux only.
How can I get icons for mimetype on macOS? Do I have to use icon(for:) and create the pixmap on my own, or are there better ways?
Solution
To get the icon for a file use QFileIconProvider::icon with QFileInfo like that:
QIcon fileIcon = QFileIconProvider().icon(QFileInfo(fileName));
Example
Here is a simple example I wrote for you to demonstrate the proposed solution:
#include <QApplication>
#include <QFileIconProvider>
#include <QFileDialog>
#include <QFileInfo>
#include <QBoxLayout>
#include <QPushButton>
class Widget : public QWidget
{
public:
Widget(QWidget *parent = nullptr) : QWidget(parent) {
auto *l = new QVBoxLayout(this);
auto *btnOpen = new QPushButton(tr("Open File"), this);
btnOpen->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
l->addWidget(btnOpen);
connect(btnOpen, &QPushButton::clicked, this, &Widget::onFileOpen);
resize(300, 300);
}
private slots:
void onFileOpen() {
const QString &fileName(QFileDialog::getOpenFileName(this, tr("Open File")));
if (fileName.isEmpty())
return;
auto *btnOpen = static_cast<QPushButton *>(sender());
btnOpen->setIcon(QFileIconProvider().icon(QFileInfo(fileName)));
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
So I needed border in QtextFrame in a pdf (QPrinter's pdf output). Following is the code I used
#include <QPainter>
#include <QTextFrameFormat>
#include <QTextCursor>
#include <QTextFrame>
#include <QTextEdit>
#include <QPrinter>
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
QTextDocument doc;
QPixmap map(1024, 1024);
map.fill(Qt::white);
QPainter p;
p.begin(&map);
p.fillRect(QRect(0,0, 4, map.height()), QBrush(Qt::red));
p.end();
QTextFrameFormat frameFormat;
QBrush bruh(map);
bruh.setColor(Qt::transparent);
frameFormat.setBackground(bruh);
frameFormat.setPadding(6);
auto cur = new QTextCursor(&doc);
auto frame = cur->insertFrame(frameFormat);
auto curf = new QTextCursor(frame);
curf->insertText("Hello this is qt program!");
QPrinter pdf;
pdf.setOutputFileName("E:\\test.pdf");
pdf.setOutputFormat(QPrinter::PdfFormat);
doc.print(&pdf);
QTextEdit edt;
edt.setDocument(&doc);
edt.show();
return a.exec();
}
But the pdf output is different from the what is shown in QTextEdit
pdf (Only a single black line no content)
Text Edit output which is what I needed
I'm using Qt but I don't know how to center a QMainWindow window. I wrote this code, but it doesn't works. Thanks in advance.
QRect screenGeometry = QApplication::desktop()->screenGeometry();
int x = (screenGeometry.width() - w->width()) / 2;
int y = (screenGeometry.height() - w->height()) / 2;
w->move(x, y); // w is a QMainWindow pointer
I get this:
These functions are obsolete:
QApplication::desktop()->screenGeometry()
QApplication::desktop()->availableGeometry()
QDesktopWidget::screen()
Use QScreen instead:
#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QScreen>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
move(screen()->geometry().center() - frameGeometry().center());
}
the following code works in the latest version of Qt 6.4.1
#include <QApplication>
#include <QScreen>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
int width = w.frameGeometry().width();
int height = w.frameGeometry().height();
QScreen *screen = a.primaryScreen();
int screenWidth = screen->geometry().width();
int screenHeight = screen->geometry().height();
w.setGeometry((screenWidth/2)-(width/2), (screenHeight/2)-(height/2), width, height);
w.show();
return a.exec();
}
Thank you to everybody. I already solved my problem, using this code.
w->setFixedSize(400, 400);
int width = w->frameGeometry().width();
int height = w->frameGeometry().height();
QDesktopWidget wid;
int screenWidth = wid.screen()->width();
int screenHeight = wid.screen()->height();
w->setGeometry((screenWidth/2)-(width/2),(screenHeight/2)-(height/2),width,height);
w->show();
I get this:
There's a shorter solution I saw somewhere:
show();
move(
QApplication::desktop()->screen()->rect().center()
- frameGeometry().center()
);
Yet, for some weird reason, the window still does not appear exactly at the center.
I created my own font dialog from QFontDialog (new SLOT added). After that, I can't set icon (*.png) to my new font dialog (with func. setWindowIcon). If I use setWindowIcon to default QFontDialog - everything is OK. So, how to set icon to my new font dialog?
FontDialog.h
#include <QtGui>
#include <QtCore>
class FontDialog: public QFontDialog {
public:
FontDialog();
~FontDialog();
public slots:
void someSlot(void);
};
FontDialog.cpp
#include "FontDialog.h"
FontDialog::FontDialog() {
}
FontDialog::~FontDialog() {
}
void someSlot(void) {
}
main.cpp
#include "FontDialog.h"
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
FontDialog *fontDialog = new FontDialog();
fontDialog->setWindowIcon(QIcon(".//icon.png")); // !!!NOT WORKING!!! for custom QFontDialog
fontDialog->show();
app.exec();
}
I'm using Qt 4.8.5 with Qt Designer 2.7.1 environment.
Thanks for any help.
The below works under Qt 4.8.4 on both OS X and Windows 7. It also works on 5.1.1 on Windows, but not on OS X :(
#include <QFontDialog>
#include <QApplication>
#include <QIcon>
#include <QPainter>
class Dialog : public QFontDialog {
public:
Dialog(QWidget *parent = 0) : QFontDialog(parent) {}
Dialog(const QFont & initial, QWidget *parent = 0) : QFontDialog(initial, parent) {}
};
QIcon myIcon(const QColor & color)
{
QIcon icon;
QPixmap pm(128, 128);
pm.fill(Qt::transparent);
QPainter p(&pm);
p.translate(64, 64);
p.scale(50, 50);
p.setBrush(color);
p.setPen(QPen(Qt::lightGray, 0.1));
p.drawEllipse(-1, -1, 2, 2);
icon.addPixmap(pm);
return icon;
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
a.setWindowIcon(myIcon(Qt::red));
Dialog d;
d.setWindowIcon(myIcon(Qt::blue));
d.show();
return a.exec();
}
I want to animate small (100x20) image by changing the color of its pixels by the same value. For example, increase red-channel value by 1 every frame and then decrease back. The image has alpha channel, the animation speed is 30...100 fps (platform dependent; 30 is enough for linux, but windows requires ~70 to look smooth).
As i know, drawing is faster when done in QImage, but displaying is faster with QPixmap.
I like QGraphicsEffects, and QPropertyAnimations. White doesn't colorize, but black does.
#include <QLabel>
#include <QPixmap>
#include <QGraphicsColorizeEffect>
#include <QTimerEvent>
#include <QPropertyAnimation>
#include <QShowEvent>
#include <QDebug>
class Widget : public QLabel
{
Q_OBJECT
Q_PROPERTY(qreal redness READ getRedness WRITE setRedness)
public:
Widget(QWidget *parent = 0)
{
QPixmap p(300, 300);
p.fill(Qt::black);
this->setPixmap(p);
colorize = new QGraphicsColorizeEffect();
colorize->setColor(Qt::red);
redness = 0;
colorize->setStrength(redness);
this->setGraphicsEffect(colorize);
animation = new QPropertyAnimation(this,"redness");
animation->setDuration(2000);
animation->setLoopCount(10);
animation->setStartValue(0.0);
animation->setEndValue(1.0);
animation->setEasingCurve(QEasingCurve::CosineCurve);
animation->start();
}
~Widget(){}
qreal getRedness()
{
return redness;
}
void setRedness(qreal val)
{
redness = val;
colorize->setStrength(redness);
this->update();
// qDebug() << redness;
}
public slots:
void showEvent(QShowEvent *)
{
animation->start();
}
private:
qreal redness;
QGraphicsColorizeEffect * colorize;
QPropertyAnimation * animation;
};
and here is the main.cpp
#include <QApplication>
#include "widget.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
Hope that helps.