Qt QScrollArea leftover graphic artifacts when scrolling with TranslucentBackground flag - c++

I get graphic artifacts when scrolling a qscrollarea that is not a seperate window and both the scroll area and the target widget's TranslucentBackground flag is set to true. The problem doesn't happen when the scrollarea is opened as a seperate window with null parent or Qt::Window flag.
Was this behavior intended or is this a bug?
If it is a bug, is
there a fix you know of?
I've tried many other window flags but all the ones that don't open a separate window have the same issue.
#include <QApplication>
#include <QWidget>
#include <QHBoxLayout>
#include <QPushButton>
#include <QScrollArea>
#include <QScrollerProperties>
#include <QScroller>
#include <QTouchDevice>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget *mainWindow = new QWidget;
QHBoxLayout *buttonsLayout = new QHBoxLayout;
QWidget *buttonsWidget = new QWidget();
buttonsWidget->setLayout(buttonsLayout);
buttonsWidget->setAttribute(Qt::WA_TranslucentBackground);
buttonsWidget->setAttribute(Qt::WA_NoSystemBackground);
for(int i = 0; i < 10; ++i) {
QPushButton *button = new QPushButton("Button " + QString::number(i), buttonsWidget);
buttonsLayout->addWidget(button);
}
QScrollArea *scrollArea = new QScrollArea(mainWindow);
scrollArea->setAttribute(Qt::WA_TranslucentBackground);
scrollArea->setAttribute(Qt::WA_NoSystemBackground);
scrollArea->setWidgetResizable(true);
scrollArea->setFixedHeight(100);
scrollArea->setWidget(buttonsWidget);
scrollArea->setWindowFlag(Qt::WindowStaysOnTopHint);
mainWindow->show();
scrollArea->show();
return a.exec();
}
Example artifact when, compiled with Qt5.12.1 open source version, ubuntu 16.04.

Related

Toggling a checkbox inside a QComboBox

I'm displaying checkboxes inside comboboxes using an adaptation of the following code. Unfortunately, the checkboxes do not behave exactly as expected. In contrast to a "normal" checkbox one can not click the checkbox's label to (un-)check the checkbox.
Is there a way to make the checkboxes inside the comboboxes behave exactly like a "normal" checkbox (i.e. clicking on the label (un-)checks it)?
#include <QtGui>
#include <QApplication>
#include <QComboBox>
#include <QTableView>
#include <QVBoxLayout>
#include <QListView>
#include <QCheckBox>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QStandardItemModel model;
QList<QStandardItem*> items;
for (int i = 0; i < 5; i++) {
QStandardItem* item = new QStandardItem(QString("Item %0").arg(i));
item->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
item->setData(Qt::Unchecked, Qt::CheckStateRole);
items.append(item);
}
model.appendColumn(items);
QComboBox* comboBox = new QComboBox();
comboBox->setModel(&model);
QVBoxLayout* layout = new QVBoxLayout();
layout->addWidget(comboBox);
layout->addWidget(new QCheckBox("Label"));
QWidget widget;
widget.setLayout(layout);
widget.show();
return app.exec();
}
No, you can not do that directly. However you can explicitly specify the behaviour of the label once it's clicked. Not to copy the code from another question, you can do something like in this question.

Qt custom widget not showing child widgets

I have a custom widget with some standard child widgets inside. If I make a separate test project and redefine my custom widget to inherit QMainWindow, everything is fine. However, if my custom widget inherits QWidget, the window opens, but there are no child widgets inside.
This is the code:
controls.h:
#include <QtGui>
#include <QVBoxLayout>
#include <QLineEdit>
#include <QPushButton>
class Controls : public QWidget
{
Q_OBJECT
public:
Controls();
private slots:
void render();
private:
QWidget *frame;
QWidget *renderFrame;
QVBoxLayout *layout;
QLineEdit *rayleigh;
QLineEdit *mie;
QLineEdit *angle;
QPushButton *renderButton;
};
controls.cpp:
#include "controls.h"
Controls::Controls()
{
frame = new QWidget;
layout = new QVBoxLayout(frame);
rayleigh = new QLineEdit;
mie = new QLineEdit;
angle = new QLineEdit;
renderButton = new QPushButton(tr("Render"));
layout->addWidget(rayleigh);
layout->addWidget(mie);
layout->addWidget(angle);
layout->addWidget(renderButton);
frame->setLayout(layout);
setFixedSize(200, 400);
connect(renderButton, SIGNAL(clicked()), this, SLOT(render()));
}
main.cpp:
#include <QApplication>
#include "controls.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Controls *controls = new Controls();
controls->show();
return app.exec();
}
This opens up a window with correct dimensions, but with no content inside.
Bear in mind this is my first day using Qt. I need to make this work without inheriting QMainWindow because later on I need to put this on a QMainWindow.
You're missing a top level layout:
Controls::Controls()
{
... (yoour code)
QVBoxLayout* topLevel = new QVBoxLayout(this);
topLevel->addWidget( frame );
}
Or, if frame is not used anywhere else, directly:
Controls::Controls()
{
layout = new QVBoxLayout(this);
rayleigh = new QLineEdit;
mie = new QLineEdit;
angle = new QLineEdit;
renderButton = new QPushButton(tr("Render"));
layout->addWidget(rayleigh);
layout->addWidget(mie);
layout->addWidget(angle);
layout->addWidget(renderButton);
setFixedSize(200, 400);
connect(renderButton, SIGNAL(clicked()), this, SLOT(render()));
}
Note that setLayout is done automatically when QLayout is created (using parent widget)
You'll want to set a layout on your Controls class for managing its child sizes. I'd recommend removing your frame widget.
controls.cpp
Controls::Controls()
{
layout = new QVBoxLayout(this);
.
.
.
}
main.cpp
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
MainWindow w;
w.show();
return app.exec();
}

Why does QtGUI not include all GUI elements in Qt 5

I watched VoidRealm tutorial, and he easily include QtGui and start using it! but i do the same thing and it doesnt work for me! for example my code doesnt know the QWidget until i include QLabel! or all other Gui element...
#include <QApplication>
#include <QtGui>
#include <QtCore>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget *win = new QWidget;
win->setWindowTitle("MBS");
QGridLayout *gLay = new QGridLayout;
QLabel *label1 = new QLabel("Name: ");
win->show();
return a.exec();
}
In Qt5 most of the previously known as QtGui functionality now is being called QtWidgets.
So try to write #include <QtWidgets>.

How to create right GUI widget by using Qt?

I'm writing a simple text editor by using Qt and Qt Creator. I wonder how to make right application's structure. I mean widgets. Is QMainWindow should be main widget or it can be QWidget? When I trying to specify QMainWindiw as QTextEdit's parent widget, QTextEdit is not displayed. Because of it I decided to initialize QMainWindow as QWidget's parent and QWidget became a parent widget for all another widgets. Is it a right way?
#include <QApplication>
#include <QMainWindow>
#include <QWidget>
#include <QVBoxLayout>
#include <QTextEdit>
#include <QMenuBar>
#include <QMenu>
#include <QSizePolicy>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow *mainWindow = new QMainWindow;
QMenu *fileMenu = new QMenu("File");
fileMenu->addAction("New");
fileMenu->addAction("Open");
fileMenu->addAction("Save");
fileMenu->addAction("Save as");
fileMenu->addSeparator();
fileMenu->setMaximumWidth(160);
QMenu *editMenu = new QMenu("Edit");
editMenu->addAction("Copy");
editMenu->addAction("Past");
editMenu->addAction("Cut");
editMenu->setMinimumWidth(160);
QMenuBar *mainMenu = new QMenuBar;
mainMenu->addMenu(fileMenu);
mainMenu->addMenu(editMenu);
mainMenu->addAction("Exit");
mainMenu->show();
QWidget *mainWidget = new QWidget(mainWindow);
mainWidget->move(0, 20);
mainWidget->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
QTextEdit *textEdit = new QTextEdit;
textEdit->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
QVBoxLayout *vBoxLayout = new QVBoxLayout;
vBoxLayout->addWidget(textEdit);
mainWidget->setLayout(vBoxLayout);
mainWidget->show();
mainWindow->setMenuBar(mainMenu);
mainWindow->show();
return a.exec();
}
You should use QMainWindow if you need to use one of its features: toolbars, dock widgets, main menu or status bar (see QMainWindow docs for more information). If you don't need them, you can use QWidget as your top level widget.
When working with QMainWindow, you need to set central widget using QMainWindow::setCentralWidget and add window contents to this widget, not to the QMainWindow itself.

execution of custom qdialog

I am getting an error while trying to run this Application ... the error message is:
main.cpp(11): error: expression must have class type
int r = dialog.exec(); and I am not sure why!!!
I am using qmake to generate the make file... I have added the necessary files to the *.pro file since Dialog is inherited from QDialog I should have access to the function exec!
#include <QtGui>
#include <QDialog>
#include <QtUtil.h>
#include <Mathematics.h>
#include <Pair.h>
#include "View.h"
class QMesseageBox;
class QAction;
class QDialogButtonBox;
class QLabel;
class QLineEdit;
class QPushButton;
class QTextEdit;
class Dialog : public QDialog {
Q_OBJECT
public:
Dialog() {
QHBoxLayout *layout = new QHBoxLayout;
// prevent left vertical box from growing when main window resized
layout->addStretch(1);
QLabel* lab_Layers = new QLabel(tr("Layers"));
d_inline = new QLineEdit;
d_inline->setText("50");
scene = new QGraphicsScene(0, 0, 500, 500);
view = new View;
layout->addWidget(view);
view->setScene(scene);
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addLayout(layout);
setLayout(mainLayout);
setWindowTitle(tr("VI Smooth 0.4"));
}
private slots:
// scroll the "after" window when "before" one is scrolled (so they
// remain in sync)
private:
QAction* exitAction;
QtUtil qt;
QLineEdit* d_inline;
QGraphicsScene* scene;
QGraphicsView* view;
};
main class
#include <QApplication>
#include <QMessageBox>
#include "Dialog.h"
int
main(int argc, char **argv) {
QApplication app(argc, argv);
argv++;
Dialog dialog();
// dialog.showMaximized();
int r = dialog.exec();
return 0;
}
It should look something like this. If you create a Dialog object, you need to call show(). And you also need to return app.exec() in main().
#include <QApplication>
#include <QMessageBox>
#include "Dialog.h"
int
main(int argc, char **argv) {
QApplication app(argc, argv);
argv++;
Dialog dialog;
dialog.show()
return app.exec(argc, argv);
}