I'm trying to use Qt 5.5.1 QGraphicsBlurEffect in my project, but can't make it work on Mac:
I was trying to make a change by using PerformanceHint, QualityHint or AnimationHint, but didn't succeed. Using QGraphicsColorizeEffect I had the same issue, while QGraphicsOpacityEffect and QGraphicsDropShadowEffect, as well as everything on Linux worked perfectly fine:
How can I change my project file or code to make this graphics issue go away? Is it even possible?
TEMPLATE = app
TARGET = main
QT += widgets
SOURCES += main.cpp
#include <QtWidgets>
int main(int argc, char **argv)
{
QApplication app(argc, argv);
QMainWindow window;
QTextEdit *text = new QTextEdit;
text->setReadOnly(true);
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(text);
QWidget *widget = new QWidget;
widget->setLayout(layout);
QGraphicsBlurEffect effect;
effect.setBlurRadius(3);
QLabel *test = new QLabel("TEST");
test->setGraphicsEffect(&effect);
(new QHBoxLayout(text))->addWidget(test, 0, Qt::AlignCenter);
window.setCentralWidget(widget);
window.show();
return app.exec();
}
I had a similar problem with QGraphicsOpacityEffect: blurred text and icons when it must be crisp sharp. Seems like a bug in Qt. My solution was to write my own small class containing everything I need with a proper rendering.
Well, you can try to fix Qt... good luck at reading source code of graphics effects. It is quite complicated with all it's dpr and transform matrix and rendering child widgets.
Related
I have a Qt program which was made without Qt Creator. And I want to improve the functionality: put all that i have now (QGridLayout, QLabels, QPushButtons and QCustomPlot) in one tab of QTabWidget and make another one tab with another layout and widgets. How should i do it correctly? I tried to create QTabWidget and add to it my QGridLayout with all stuff. Mb it is right way.
Tried to find some info but all are using creator for their projects. i dont like it and so i cant find examples of how can i push all ready functionality to qtabwidget
This is my main function:
int main(int argc, char **argv) {
QApplication a(argc, argv);
Smartcalc calc;
calc.resize(750, 500);
calc.show();
return a.exec();
}
This is my constructor:
Smartcalc::Smartcalc(QWidget *parent) : QWidget(parent) {
createWidgets();
initGraph(customPlot);
mainLayout = new QGridLayout;
addWidgetsToLayout(mainLayout);
setWindowTitle(tr("Smartcalc_v1.0"));
connectWidgets();
customWidgets();
}
So i need to create two tabwidgets and push my Smartcalc functionality in one of tabwidgets.
I'm trying to display a Q3DScatter graph in a Main Window designed with Qt Designer so I've added a widget to the main window but I don't know how to embed the graph object in this widget. I tried to create a widget "container" programmatically and embed the graph in it and then putting the widget in a QHBoxLayout (which has been added to the main window using Qt Designer) using the following code in my main window's constructor:
...
Q3DScatter *graph = new Q3DScatter();
QWidget *container = QWidget::createWindowContainer(graph);
ui->horizontal_layout->addWidget(container, 1);
But the window appears to be empty when executing. I'd really appreciate some help.
EDIT: Here's the full code of my main window constructor:
ResultsWindow::ResultsWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::ResultsWindow)
{
ui->setupUi(this);
Q3DScatter *graph = new Q3DScatter();
QWidget *container = QWidget::createWindowContainer(graph);
ui->horizontal_layout->addWidget(container, 1);
}
And here's the main code:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ResultsWindow w;
w.show();
return a.exec();
}
EDIT 2: I forgot to specify that the horizontal layout is embedded in a GridLayout. I tried to create a new project and my code actually works perfectly when I add a horizontal layout directly to the main window. So could the problem be due to the GridLayout?
I want to have some sequential actions; for example press a QPushButton and then delete the layout that is running and run another layout in the "SAME WINDOW"
In fact, I don't know what exactly layouts and widgets are!
Are they an object? an instance of object or what?
I found the bellow code in the Internet, I don't know how to change it to make it useful for me
#include <QApplication>
#include <QPushButton>
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QWidget *window = new Qwidget;
QPushButton *button1 = new QPushButton("One");
QPushButton *button2 = new QPushButton("Two"); QPushButton *button3 = new QpushButton("Three");
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(button1);
layout->addWidget(button2);
layout->addWidget(button3);
window->setLayout(layout);
window->show();
return app.exec();
}
A better way than deleting layout and setting a new one would be to use a QStackedWidget (docs) and the concept of pages. Using QStackedWidget you can show and hide pages as you wish.
Can some one explain me how to make a window in qt according to the shape of some object in an image , for example i have an image of a tree , using that i need to create a window in the shape of a tree ..
After a long search , myself found a good solution , check out this ..
#include <QtGui>
class myMainWindow:public QMainWindow
{
public:
myMainWindow():QMainWindow()
{
setMask((new QPixmap("saturn.png"))->mask());
QPalette* palette = new QPalette();
palette->setBrush(QPalette::Background,QBrush(QPixmap("saturn.png")));
setPalette(*palette);
setWindowFlags(Qt::FramelessWindowHint);
QWidget *centralWidget = new QWidget(this);
QGridLayout *layout = new QGridLayout();
centralWidget->setLayout(layout);
QPushButton* button1 = new QPushButton("Button 1");
button1->setFixedSize(80,50);
layout->addWidget(button1,0,0);
setCentralWidget(centralWidget);
};
~myMainWindow(){};
};
int main(int argc, char **argv)
{
QApplication app(argc, argv);
myMainWindow *window = new myMainWindow();
window->resize(600, 316);
window->show();
return app.exec();
}
Here is a recipe for making a widget with a semi-transparent background colour. Just expand from there by making the background fully transparent, then display the tree image on top of that as a background image. Note that the widget will still behave like a rectangular widget in regards to laying out its child elements, so you probably need to deal with this using some custom layout inside the tree shape.
Start from the docs for QWidget::setMask. It has a version which takes a QBitmap and one that takes a QRegion. This is the fundamental function in getting a transparent widget. The toolkit also includes a clock example using the QRegion version -- I suspect a bitmap is just as easy though.
I am trying to add a label to the main window using Qt. Here is a piece of the code:
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QWidget Main_Window;
QPixmap Image;
Image.load("1837.jpg");
QLabel i_label;
i_label.setPixmap(Image);
i_label.show();
QPushButton Bu_Quit("Quit", &Main_Window);
QObject::connect(&Bu_Quit, SIGNAL(clicked()), qApp, SLOT(quit()));
Main_Window.show();
return app.exec();
}
I've been having a very hard time figuring out how to properly add QLabels to QWidgets, I tried to set the Main_Window as the main widget using this method: app.setMainWidget(Main_Window) and the label was still outside the window. So how do I put labels into widgets using Qt?
hamza, this code worked fine for me:
#include <QtGui>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget Main_Window;
QLabel i_label("Start", &Main_Window);
//i_label.setPixmap(QPixmap("1837.jpg"));
QPushButton Bu_Quit("Quit" , &Main_Window);
QObject::connect(&Bu_Quit , SIGNAL(clicked()), qApp , SLOT(quit()));
QVBoxLayout *vbl = new QVBoxLayout(&Main_Window);
vbl->addWidget(&i_label);
vbl->addWidget(&Bu_Quit);
Main_Window.show();
return app.exec();
}
I commented setting the image code to show you that the label was set correctly. Make sure your image is valid (otherwise you won't see the text). The trick here was that you need to use qt layouts like QVBoxLayout
Add the label to a layout widget and set the window layout to that layout.
Design note: its better to create your own MainWindow class, inheriting from QMainWindow for instance, and design it from the inside.
or even better, use QtCreator.
You can try:
QWidget window;
QImage image("yourImage.png");
QImage newImage = image.scaled(150, 150, Qt::KeepAspectRatio);
QLabel label("label", &window);
label.setGeometry(100, 100, 100, 100);
label.setPixmap(QPixmap::fromImage(newImage));
window.show();
this way you can even decide where to put the label and choose the image size.