The code below loads an image using QLabel. The code uses: myLabel.setMask(pixmap.mask()); and it works as it should. The problem comes when I try and load an image using QGraphicsScene.
#include <QApplication>
#include <QLabel>
#include <QtGui>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QLabel myLabel;
QPixmap pixmap("/path/tomy/image.jpg");
myLabel.setPixmap(pixmap);
myLabel.setMask(pixmap.mask());
myLabel.show();
return app.exec();
}
In this code I am attempting to the same as above but using QGraphicsScene. The pixmap is loaded properly, after that I am not sure why the program is not working properly. Is it because there is no setMask() operation? Or is there an operation missing that is needed to make the image visible?
#include <QtGlobal>
#if QT_VERSION >= 0x050000
#include <QtWidgets>
#else
#include <QtGui>
#endif
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QPixmap pixmap("/path/tomy/image.jpg");
QGraphicsPixmapItem item( pixmap);
QGraphicsScene* scene = new QGraphicsScene;
scene->addItem(&item);
QGraphicsView view(scene);
view.show();
return a.exec();
}
Related
I know it is possible with QPrintPreviewWidget via currentPage() function, but is there a way to return current page in QPrintPreviewDialog? Since I like the default QPrintPreviewDialog's interface, and I don't feel confident enough to rebuild it myself, I would like to use QPrintPreviewDialog.
QPrintPreviewDialog is a QDialog that has a QPrintPreviewWidget as internal elements, so using findChild you can obtain that object.
#include <QApplication>
#include <QPrintPreviewDialog>
#include <QPrintPreviewWidget>
#include <QPrinter>
#include <QTimer>
#include <QTextCursor>
#include <QTextDocument>
#include <QDebug>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QPrintPreviewDialog previewDialog;
QObject::connect(&previewDialog, &QPrintPreviewDialog::paintRequested, &previewDialog, [&previewDialog](QPrinter *printer){
QTextDocument document;
QTextCursor cursor(&document);
QTextBlockFormat blockFormat;
for(int i=0; i < 10; i++){
cursor.insertBlock(blockFormat);
cursor.insertHtml(QString("<h1>This is the %1 page</h1>").arg(i+1));
blockFormat.setPageBreakPolicy(QTextFormat::PageBreak_AlwaysBefore);
}
document.print(printer);
if(QPrintPreviewWidget *previewWidget = previewDialog.findChild<QPrintPreviewWidget *>()){
qDebug() << previewWidget->currentPage();
// change page
QTimer::singleShot(100, previewWidget, [previewWidget](){
previewWidget->setCurrentPage(2);
});
}
});
previewDialog.exec();
}
I am making a very simple application which plays a video using QMediaPlayer/QGraphicsVideoItem/QGraphicsScene/QGraphicsView and in the meantime, it displays a web page using QWebEngineView.
However, when the QWebEngineView shows, only the sound of the video is played. The widget shows only white. When QWebEngineView's ->show() is not called, the video plays normally.
I've tried QVideoWidget. It doesn't have such problem.
I'm thinking that the rendering of the QGraphicsView might have conflicts with QWebEngine's.
Here's the code:
#include "mainwindow.h"
#include <QApplication>
#include <QWebEngineView>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QMediaPlayer>
#include <QVideoWidget>
#include <QGraphicsVideoItem>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWebEngineView *webview = new QWebEngineView();
QMediaPlayer *player = new QMediaPlayer();
QGraphicsVideoItem *item = new QGraphicsVideoItem;
QVideoWidget *vwid = new QVideoWidget();
QGraphicsView *gview = new QGraphicsView();
player->setMedia(QUrl::fromLocalFile("/home/user/Videos/IMG_6201.MOV"));
//player->setVideoOutput(vwid);
player->setVideoOutput(item);
QGraphicsScene *gscene = new QGraphicsScene();
gview->setScene(gscene);
webview.setUrl(QUrl("https://www.google.com"));
gview->scene()->addItem(item);
//vwid->show();
player->play();
webview->show();
gview->show();
return a.exec();
}
I want to create a PDF-file in Qt console application. But file size of created file is 0B and I can't open it.
The same code in GUI application works. Where is the difference between code in console and gui application? What should I do to make the code working in console application?
Thanks for help in advance!
#include <QCoreApplication>
#include <QPrinter>
#include <QTextDocument>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString html = "<h1>Hi!</h1>";
QTextDocument document;
document.setHtml(html);
QPrinter printer(QPrinter::PrinterResolution);
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setOutputFileName("test.pdf");
document.print(&printer);
return a.exec();
}
Here it's working.
#include <QApplication>
#include <QPrinter>
#include <QTextDocument>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QString html = "<h1>Hi!</h1>";
QTextDocument document;
document.setHtml(html);
QPrinter printer(QPrinter::PrinterResolution);
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setOutputFileName("test.pdf");
document.print(&printer);
return a.exec();
}
so it looks like you have just to change the QCoreApplication by QApplication.
I am writing a QtQuick desktop application. I use both c++ (for functionality) and QML (for UI) in it.
I use QQuickView to show the interface written in QML.
I want this application to reside in System Tray when minimised.
I mean a functionality similar to this example. http://qt-project.org/doc/qt-4.8/desktop-systray.html .
I am trying to implement this feature but could not find a way to do this in my Qt Quick application.
Here is my main.cpp code:
#include <QGuiApplication>
#include <QQmlEngine>
#include <QQmlContext>
#include <QQmlFileSelector>
#include <QQuickView>
#include "myapp.h"
int main(int argc, char* argv[])
{
QGuiApplication app(argc,argv);
app.setApplicationName(QFileInfo(app.applicationFilePath()).baseName());
QDir::setCurrent(qApp->applicationDirPath());
MyApp myappObject;
QQuickView view;
view.connect(view.engine(), SIGNAL(quit()), &app, SLOT(quit()));
view.rootContext()->setContextProperty("myappObject", &myappObject);
new QQmlFileSelector(view.engine(), &view);
view.setSource(QUrl("qrc:///myapp.qml"));
view.setResizeMode(QQuickView::SizeRootObjectToView);
view.show();
return app.exec();
}
Please help by providing any hint/pointers to do this.
Thanks.
I was facing the same challenge today and ended up using the following solution within main(). Works great for me when using Qt 5.3. You should of course implement a better way to check whether the first root object is your application window object or not.
#include <QApplication>
#include <QQmlApplicationEngine>
#include <QMessageBox>
#include <QAction>
#include <QMenu>
#include <QSystemTrayIcon>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
if (!QSystemTrayIcon::isSystemTrayAvailable()) {
QMessageBox::critical(0, QObject::tr("Systray"),
QObject::tr("I couldn't detect any system tray "
"on this system."));
return 1;
}
QApplication::setQuitOnLastWindowClosed(false);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
QObject *root = 0;
if (engine.rootObjects().size() > 0)
{
root = engine.rootObjects().at(0);
QAction *minimizeAction = new QAction(QObject::tr("Mi&nimize"), root);
root->connect(minimizeAction, SIGNAL(triggered()), root, SLOT(hide()));
QAction *maximizeAction = new QAction(QObject::tr("Ma&ximize"), root);
root->connect(maximizeAction, SIGNAL(triggered()), root, SLOT(showMaximized()));
QAction *restoreAction = new QAction(QObject::tr("&Restore"), root);
root->connect(restoreAction, SIGNAL(triggered()), root, SLOT(showNormal()));
QAction *quitAction = new QAction(QObject::tr("&Quit"), root);
root->connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
QMenu *trayIconMenu = new QMenu();
trayIconMenu->addAction(minimizeAction);
trayIconMenu->addAction(maximizeAction);
trayIconMenu->addAction(restoreAction);
trayIconMenu->addSeparator();
trayIconMenu->addAction(quitAction);
QSystemTrayIcon *trayIcon = new QSystemTrayIcon(root);
trayIcon->setContextMenu(trayIconMenu);
trayIcon->setIcon(QIcon(":/resources/DatagnanLogoColor.png"));
trayIcon->show();
}
return app.exec();
}
Copy the Windos class (window.cpp/window.h) from systray example to your project, port it to Qt5 if necessary and open both from your main file:
int main(int argc, char* argv[])
{
// ...
QQuickView view;
// ...
view.show();
Window window;
window.show();
return app.exec();
}
.h
#include <QObject>
#include <QDebug>
class MyClass : public QObject
{
Q_OBJECT
public:
Q_INVOKABLE void cppMethod (const QString &msg)
{
qDebug() << "Called the C++ method with" << msg;
}
public slots:
void cppSlot (int number)
{
qDebug() << "Called the C++ slot with" << number;
}
};
.cpp
#include <QtCore/QCoreApplication>
#include <QDeclarativeEngine>
#include <QDeclarativeComponent>
#include <QDeclarativeContext>
#include <QDeclarativeView>
#include <QVariant>
#include <QMetaObject>
#include "cppFromQml.h"
int main (int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QDeclarativeView view;
return a.exec();
}
This results in segmentation fault. What's the way out?
Qt: 4.8.1
note that you're not using MyClass, and - just my guess - a declarative view will need a QApplication to properly run.
To better understand, I created a project, dumped almost all away (just kept the .pro, where I added qt += declarative), and changed a bit your code as follow:
#include <QApplication>
#include <QDeclarativeEngine>
#include <QDeclarativeComponent>
#include <QDeclarativeContext>
#include <QDeclarativeView>
#include <QVariant>
#include <QMetaObject>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QDeclarativeView view;
view.show();
return a.exec();
}
now it runs and display an empty view, as expected...