Change property of random element in QListWidget - qt

I have 5 QProgressBars in a QListWidget (ui->listWidget). How can I access the third QProgressBar element and change its value ex. ( progressBar->setValue(40) )
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
a = new QPushButton(this);
connect(a, SIGNAL (clicked()),this, SLOT (clickedSlot()));
}
void MainWindow::clickedSlot()
{
QProgressBar *prog = new QProgressBar(this);
QListWidgetItem *it;
it = new QListWidgetItem(ui->listWidget);
ui->listWidget->insertItem(ui->listWidget->size().height(),it);
it->setSizeHint(QSize(200,50));
ui->listWidget->setItemWidget(it, prog);
}

The following code will obtain the third element in the list and set the progress to 40%.
QProgressBar *bar = qobject_cast<QProgressBar*>(ui->listWidget->itemWidget(pList->item(2)));
if (bar)
bar->setValue(40);
qobject_cast will safely cast the QWidget to QProgressBar, only if the widget is indeed a QProgressBar. If you are sure the third element is a QProgressBar, you can omit the if test if(bar).
See the qt documentation QListWidget and qobject_cast for more information.

Create the definition for the QProgressBar(s) in the class header file, then you can connect things to the setValue slot, or access them directly.
It seems odd to be adding ProgressBars to QListWidgetItems... wouldn't QHBoxLayout be more suitable?

Related

Button is not showing on main window even after successful execution of code in Qt

I have tried this code but the button isn't displaying on the main window.
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QGridLayout>
#include<QLabel>
#include<QPushButton>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QPushButton *l=new QPushButton();
l->setText("abc");
QGridLayout *q=new QGridLayout();
q->addWidget(l);
this->setLayout(q);
this->show();
}
MainWindow::~MainWindow()
{
delete ui;
}
I have tried to change the code even by passing enums for alignment but nothing worked.
When you create new Qt widgets Application, the default form (MainWindow ui) is created with centralWidget to put all other widgets. In your code you created the QGridLayout without a parent, typically such layout should be placed in ui->centralWidget (as far as you are not creating another widget to be set as centralWidget), moreover I assume your mainWindow is shown from main.cpp (need not use show()). your code could thus be:
QPushButton *l=new QPushButton();
l->setText("abc");
QGridLayout *q=new QGridLayout(ui->centralWidget);
q-> addWidget(l);
Try adding the widget to the GridLayout with index using addWidget function
void QGridLayout::addWidget(QWidget *widget, int row, int column, Qt::Alignment alignment = ...)
like:
q-> addWidget(l, 0, 0);
P.S. also consider using better names for your variables!

QDockWidget will not show contents

I am attempting to create a specialised QDockWidget, with some contents, that I can use in other Qt5 UIs. To do this, I have created a QDockWidget subclass complete with a UI form, called SpecialDockWidget. The contents (a QTreeWidget) are added in the UI form.
In my main UI, I have added SpecialDockWidget as a QDockWidget promotion, have promoted the desired dock widget, and everything compiles OK. However, when I run the application my promoted dock widget contains the title of the SpecialDockWidget in its title bar, but the contents remain empty. This only happens if the dock widget is made part of the main UI (whether floating or attached), and the dock widget displays the contents successfully if it is instantiated on its own as a top-level widget.
The Qt documentation says the following with regards to QDockWidget::setWidget():
If the dock widget is visible when widget is added, you must show() it explicitly.
Note that you must add the layout of the widget before you call this function; if not, the widget will not be visible.
I have tried calling show() on both the SpecialDockWidget and the contents, and have set the layout myself, but the contents still don't show up.
Can anyone tell me what I might be doing wrong?
EDIT: Some photos showing what's going on:
Dock widget code:
#include "specialdockwidget.h"
#include "ui_specialdockwidget.h"
#include <QVBoxLayout>
SpecialDockWidget::SpecialDockWidget(QWidget *parent) :
QDockWidget(parent),
ui(new Ui::SpecialDockWidget)
{
ui->setupUi(this);
}
SpecialDockWidget::~SpecialDockWidget()
{
delete ui;
}
Main window code:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// This produces the top-level dock widget
// SpecialDockWidget* w = new SpecialDockWidget();
// w->show();
}
MainWindow::~MainWindow()
{
delete ui;
}
The QDockWidget is created in the Designer so it's not very clear how the tree is added to the QDockWidget. If the tree is simply a child of the QDockWidget it's not correct. You must add the tree to the QDockWidget using QDockWidget::setWidget().
I suggest to not use the Designer to create a subclass of QDockWidget because it seems not possible to add the content correctly. You can do something like this instead.
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
QDockWidget *dw = new QDockWidget(this);
QTreeWidget* tw = new QTreeWidget(dw);
dw->setWidget(tw);
addDockWidget(Qt::LeftDockWidgetArea, dw);
dw->show();
}
Of course, if you need to put inside a dock widget a more complex widget, you can create the content with the Designer as a simple QWidget subclass and add it to the dock widget in the same way.
I think your main window's constructor should look like, for example:
MainWindow::MainWindow(QWidget *parent)
:
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
SpecialDockWidget *w = new SpecialDockWidget();
// Add dock widget to the specified area of main window.
addDockWidget(Qt::LeftDockWidgetArea, w);
}

How to create resizable cusom widget on the QGraphicsScene

I study QGraphics framework and want to create custom resizable widget.
For example I created a proxy widget with QTextEdit
QGraphicsLinearLayout* l = new QGraphicsLinearLayout;
QGraphicsProxyWidget* proxy = new QGraphicsProxyWidget;
proxy->setWidget( new QTextEdit );
proxy->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Preferred );
l->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Preferred );
l->addItem( proxy );
QGraphicsWidget* w = new QGraphicsWidget;
w->setLayout( l );
w->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Preferred );
w->setFlag( QGraphicsItem::ItemIsMovable );
scene->addItem( w );
Widget looks fine, but I can't find out how add ability to resize it.
I searched in the Qt Examples, and google, but can't find any example.
A GraphicsItem's size, of which QGraphicsProxyWidget derives, is defined by its bounding rectangle. I expect the size of the widget would define the initial size of its proxy widget, so you could try changing the actual widget first.
In order to change the QGraphicsItem's size, you'd need to derive from QGraphicsProxyWidget and override its boundingRect() function.
Then you'd be able to create a resize function to change the returned rectangle, but ensure you call prepareGeometryChange first.
If you do inherit from QGraphicsProxyWidget and change its size this way, the enclosed widget may or may not be resized, depending upon its implementation.
I suggest you start by trying to resize the enclosing widget first.
Also note that there exists a setScale function for QGraphicsItems, which may also be an option here, as well as being able to scale the QPainter, in the paint function, if you derive from QGraphicsProxyWidget.
You should have your QGraphicsView instance resizable, so your scene content will react on resizing too.
Here it's working example (you should add QGraphicsView element on your MainWindows form):
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTextEdit>
#include <QPushButton>
#include <QGraphicsLinearLayout>
#include <QGraphicsProxyWidget>
#include <QGraphicsScene>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QGraphicsScene* scene = new QGraphicsScene(this);
QGraphicsWidget *textEdit = scene->addWidget(new QTextEdit);
QGraphicsWidget *pushButton = scene->addWidget(new QPushButton);
QGraphicsLinearLayout *layout = new QGraphicsLinearLayout;
layout->addItem(textEdit);
layout->addItem(pushButton);
QGraphicsWidget *form = new QGraphicsWidget;
form->setLayout(layout);
scene->addItem(form);
ui->graphicsView->setScene(scene);
setCentralWidget(ui->graphicsView);
}
MainWindow::~MainWindow()
{
delete ui;
}
However this doesn't affect sizing of the widgets inside scene. To achieve that see Merlin's answer.
Also here it's example of working solution with using of scaling obtained from here:
void MyGraphicsView::resizeEvent(QResizeEvent* event)
{
QGraphicsView::resizeEvent(event);
QSize viewportSize = this->viewport().size();
QSize imageGridSize = ...; //size of all images (bounding rect)
qreal factor = viewportSize.width() / imageGridSize.width();
if( viewportSize.height() < (imageGridSize * factor).height() )
factor *= viewSize.height() / (imageSize * factor).height();
this->resetTransform();
QTransform transform = this->transform();
transform.scale(factor, factor);
this->setTransform(transform);
}

QDockWidget Content bug with titlebar

Here is my problem: I created a QDockWidget and put a QGraphicsView in it, while it is floating everything shows up correctly, but if Dock it somewhere, the starting X and Y of my content is on the titlebar of the QDockWidget. Obviously, it should be under:
tilesetWindow = new QDockWidget(tr("Tileset"), this);
tilesetWindow->setMinimumSize(256,256);
tilesetWindow->setFloating(true);
connect(tilesetWindow, SIGNAL(visibilityChanged(bool)), this, SLOT(triggeredTileset()));
tilesetViewer = new QGraphicsView(tilesetWindow);
tilesetViewer->resize(256,256);
tilesetViewer->show();
An image to illustrate this: http://img86.xooimage.com/files/d/7/6/problem-391a96a.png
I've tried to put the QGraphicsView in a container and then in the QDockWidget but had the same result as above. it's the only place where I write code for the QDockWidget.
How can I make it start at the right place when it is Docked?
Edit:
I tried in a new QtProject and made a QDockWidget with a QTextBrowser in it and had the same bug:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QDockWidget *dock = new QDockWidget(this, 0);
dock->setFloating(true);
QTextBrowser *t = new QTextBrowser(dock);
t->show();
}
I'm missing something I think...
Qt Creator 2.4.1
Based on Qt 4.7.4 (32 bit)
After setting the QDockWidget as the Parent, you also need to set the QDockWidget's Widget as the QGraphicsView, or any other you want.
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QDockWidget *dock = new QDockWidget(this, 0);
dock->setFloating(true);
QTextBrowser *t = new QTextBrowser(dock);
dock->setWidget(t);
}

QT4 using QMdiArea and QScrollArea strange usage trouble

Here is what I am doing: mainwindow with MdiArea, and I add a scrollarea widget (which contains a image label) to MdiArea as a subwindow. It doesn't work (the picture doesn't show).
Here is my code:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QScrollArea sa;
QPixmap *image = new QPixmap("2.jpg");
QLabel* imageLabel = new QLabel();
imageLabel->setPixmap(*image);
sa.setWidget(imageLabel);
sa.show();
ui->mdiArea->addSubWindow(&sa);
}
But when I use a QLabel as subwindow directly, i.e. replace the last line with:
ui->mdiArea->addSubWindow(imageLabel);
it works perfectly.
Anyone know why this is happening?
QScrollArea sa;
This declares a QScrollArea on the stack. It gets destroyed immediately after the constructor finishes. Allocate it with new like you do for the other widgets and it should start working.
QScollArea *sa = new QScrollArea;
...
ui->mdiArea->addSubWindow(sa);
(And change the sa. to sa->.)

Resources