I'm a beginner in Qt, currently reading this : https://zetcode.com/gui/qt5/menusandtoolbars/
When I declare QActions in a QToolBar, the QPixmap objects (turned into QIcons) are not showing :
No icons, only text
However, the QPixmap images are actually showing when I declare a QMenu without the toolbar.
I am using Qt6 ; working on Fedora ; no warning shown on my compiler.
simple_menu.hpp
#ifndef SIMPLE_MENU_HPP
#define SIMPLE_MENU_HPP
#include <QMainWindow>
#include <QApplication>
class SimpleMenu : public QMainWindow
{
public:
SimpleMenu(QWidget *parent = nullptr);
};
#endif
simple_menu.cpp
#include "simple_menu.hpp"
#include <QMenu>
#include <QMenuBar>
#include <QToolBar>
#include <QIcon>
#include <QAction>
SimpleMenu::SimpleMenu(QWidget *parent)
: QMainWindow(parent)
{
QPixmap newpix("new.png");
QPixmap openpix("open.png");
QToolBar *toolbar = addToolBar("main toolbar");
toolbar->addAction(QIcon(newpix), "New File");
toolbar->addAction(QIcon(openpix), "Open File");
}
main.cpp
#include "simple_menu.hpp"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
SimpleMenu window;
window.resize(350, 250);
window.setWindowTitle("Tuto Toolbar");
window.show();
return app.exec();
}
Maybe the system cannot find your pictures. Please try to put the full file path (like :).
QPixmap newpix("c:\mydoc\pictures\new.png");
To check if the pictures are loaded, you can do something a bit different :
QPixmap newpix;
qDebug() << newpix.load("c:\mydoc\pictures\new.png");
the load method returns true or false in case of loading fail.
Anyway, if you want to embed your icons in the final EXE file, check the Qt resource system
It's a convenient system to achieve what you want to do, and to avoid dealing with storage paths on your local drives.
Good luck !
Related
I am working on a simple project where I want to know the available printers, then connect to one of them and see if there are errors or not.
I have used QPrinterInfo class in order to know the available printer names, then in order to work with one of the available printers, I set its name using "printer.setPrinterName("desired printer name")".
In order to know if there are some errors related to this printer (named as desired printer name), I searched and find out I have to use QPrinter::Error and then handle the errors.
My question is that what kinds of error can occur when using QPrinter::Error inside my if statement(in the following code)? If no papers exists inside the printer, will using QPrinter::Error throw an error? In general, how can I know what kinds of error I can handle and how to handle them?
code:
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtPrintSupport/QPrinter>
#include <QtPrintSupport/QPrinterInfo>
#include <QtPrintSupport/QPrintDialog>
#include <QList>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
QPrinter printer;
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QPrinterInfo PrinterInfo;
qDebug() << QStringList(QPrinterInfo::availablePrinterNames());
if (QPrinterInfo::availablePrinterNames().isEmpty())
{
qDebug() << " printer not found ";
}
printer.setPrinterName("desired printer name");
if (printer.printerState() == QPrinter::Error)
{
qDebug() << "there is an error";
}
}
MainWindow::~MainWindow()
{
delete ui;
}
main.cpp:
#include "mainwindow.h"
include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
Followings are my experience with QPrinter in Linux which built on CUPS. So if you are planning to use it on Windows it might be a totally difference experience than mine. But I think error checking in Linux contains a lot of uncertainties.
I tried QPrinter::printerState() with HP 107w, it was always returning Idle even when printer is not plugged in. Same was happening with Xerox WorkCentre 3025 too. So I recommend you to not rely on QPrinter::PrinterState. Also documentation states that QPrinter::printerState
Returns the current state of the printer. This may not always be accurate (for example if the printer doesn't have the capability of reporting its state to the operating system).
I was printing to via QPainter.begin(printer). It only fails if printer does not connected when OS started. But once the printer available after a boot, it behaves like everything is going to be okay forever. It just queues up prints and when printer becomes available, fed up with paper, just prints everything it received before. I thought return value of QPrinter::newPage() would be good place to check if there is no paper and ask user to insert papers to continue. But nah, it never returns false neither.
I have applied a dark style to my application, so good until then, my question is why the style is not applied to the title bar of my application, and the rest of the forms that open in me application, as you can see, it stays white and looks very bad, any suggestions would be appreciated.
#include "mainwindow.h"
#include <QApplication>
#include <QFile>
#include <QTextStream>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QFile f(":/qdarkstyle/style.qss");
f.open(QIODevice::ReadOnly|QIODevice::Text);
QTextStream ts(&f);
a.setStyleSheet(ts.readAll());
MainWindow w;
w.show();
return a.exec();
}
I got the subject from here.
https://github.com/ColinDuquesnoy/QDarkStyleSheet
[Works on Qt 6.2.2, not sure about Qt5]
There might be a configuration file qt.conf in the same folder as your executable file. If it does not exist, create. And add the lines
[Platforms]
WindowsArguments = darkmode=1
Use darkmode=2 if you also want to change the title bar colour on the fly.
My animation isn't working on this QPushButton quite the way I'm expecting.
Here's my mainwindow.cpp, as you can see nothing particularly weird here. On runtime the button appears as one might expect. I didn't have any particular movement in mind. I just wanted to see if I had set everything up correctly. As things are right now the button doesn't grow or move. What's strange here is that if I comment out the setShape.start() it defaults back to the size designated in the UI file.
My only guess thus far is that my MainWindow object isn't being displayed until after its constructor (containing the animation) has run its course. If this is the case, I'm wondering what I can do to get around it.
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QLabel>
#include <QPushButton>
#include <QPropertyAnimation>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QPropertyAnimation setShape(ui->pushButton, "geometry");
setShape.setDuration(1000);
setShape.setStartValue(QRect(500,300,500,500));
setShape.setEndValue(QRect(800,400,500,500));
setShape.start();
}
MainWindow::~MainWindow()
{
delete ui;
}
Here's my main.cpp
#include "mainwindow.h"
#include <QApplication>
#include <QPropertyAnimation>
#include <QPushButton>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
This is about as basic an animation as one could create, so I'm hoping that either my intuition on this is correct, or I'm making a silly mistake. Either way any help would be greatly appreciated.
A local variable is deleted after it finishes executing its scope, and that is what happens with setShape, it is eliminated when the constructor finishes executing, what you have to do is create a pointer so that it is maintained and establish the policy of destruction to DeleteWhenStopped:
QPropertyAnimation *setShape = new QPropertyAnimation(ui->pushButton, "geometry");
setShape->setDuration(1000);
setShape->setStartValue(QRect(500,300,500,500));
setShape->setEndValue(QRect(800,400,500,500));
setShape->start(QPropertyAnimation::DeleteWhenStopped);
I've created a simple Qt Widgets application using Qt Creator on my Windows 10 machine. I use the ui file and the designer to change properties of my QMainWindow, but somehow the width, height and windowTitle properties have no effect when I set them in the designer. Example:
However, the resulting application looks like this:
Both size and windowTitle are seemingly ignored. I've also tried setting properties from code, like this (but to no avail):
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setWindowTitle("That's some title you've got there");
}
I have also added a layout to the centralWidget but that only had an effect on the child controls, not the actual window itself (and that seems logical).
Normally, it should work.
Can you try if the following minimal example works for you?
#include <QApplication>
#include <QMainWindow>
main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow m;
m.setWindowTitle("TEST");
m.show ();
a.exec();
}
I have the following piece of code:
#include <QtWidgets/QtWidgets>
#include <QtMultimedia/QCamera>
#include <QtMultimedia/QMediaPlayer>
int main(int argc, char * argv[])
{
QApplication testQt(argc, argv);
QMainWindow w;
QWidget videoContainer(&w);
w.setCentralWidget(&videoContainer);
QVideoWidget videoWidget(&videoContainer);
QCamera cam(&w);
cam.setViewfinder(&videoWidget);
cam.start();
w.show();
return testQt.exec();
}
in which I am trying to create a main window, create a container widget to display video, create a videowidget in that container, and then finally set the viewfinder of the camera to that videowidget. However, when I try to do this I get the error
Variable has incomplete type 'QVideoWidget'
Why am I getting this error?
You need to include the corresponding header as follows:
#include <QVideoWidget>
You may also need to add this to your project file:
QT += multimediawidgets