How should I center my QLabel derived widget papyrus inside a QScrollArea?
QScrollArea *scroll_area = new QScrollArea(this);
scroll_area->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
scroll_area->setWidgetResizable(true);
scroll_area->setBackgroundRole(QPalette::Dark);
papyrus = new Papyrus(scroll_area);
scroll_area->setWidget(papyrus);
setCentralWidget(scroll_area);
resize(800, 600);
This is the snippet I am using but my widget sticks to the top left...
(main.cpp example):
#include <QApplication>
#include "MainWindow.h"
#include <QScrollArea>
#include <QLabel>
#include <QHBoxLayout>
int main(int argc, char** argv)
{
QApplication a(argc, argv);
QLabel label("Label");
label.setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
QScrollArea area;
area.setWidgetResizable(true);
area.setWidget(&label);
area.show();
return a.exec();
}
Related
How to show more number(folder) of images in Qlabel or QScrollArea?
QImage image("E:/Raul/Images");
ui.label->setPixmap(QPixmap::fromImage(image));
Like this but i want more number images will load in one label.
Result:
Code:
#include <QApplication>
#include <QLabel>
#include <QLineEdit>
#include <QPointer>
#include <QPushButton>
#include <QVBoxLayout>
#include <QWizardPage>
#include <QDebug>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget widget;
QVBoxLayout *layout=new QVBoxLayout();
QLabel *label=new QLabel("<img src=:/0_0.jpg align=middle><img src=:/0_1.jpg align=middle><strong>Hello</strong> "
"<font color=red>Sai Raul!");
layout->addWidget(label);
widget.setLayout(layout);
widget.show();
return a.exec();
//
}
QLabel is not a web browser, therefore hyperlinks like <img src=/media/cc0-images/grapefruit-slice-332-332.jpg/> doesn't work, but why images from resources can not do it))).
I have a QTreeView and I can't find a way of making it fill the whole dialog window and resize with the window when it is resized.
Something like this:
#include <QApplication>
#include <QDialog>
#include <QHBoxLayout>
#include <QTreeView>
class MyDialog: public QDialog
{
public:
MyDialog()
{
QHBoxLayout* l = new QHBoxLayout(this);
setLayout(l);
QTreeView* v = new QTreeView(this);
l->addWidget(v);
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyDialog d;
d.exec();
return a.exec();
}
I'd like to show and then close a dialog after 5 seconds. The dialog needs to be automatically resized (horizontally and vertically) based on the content of a label. Here is my code:
#include <QApplication>
#include <QDialog>
#include <QLabel>
#include <QTimer>
void notify (int intTime=1000)
{
QDialog notify;
notify.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
notify.setWindowFlag(Qt::FramelessWindowHint);
QLabel *lbl = new QLabel(¬ify);
lbl->setText("This is a test This is a test This is a test This is a test This is a test This is a test This is a test");
QApplication::processEvents();
notify.adjustSize();
QTimer::singleShot(intTime, ¬ify, SLOT(close()));
notify.exec();
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
notify(5000);
exit(0);
// return a.exec();
}
It does not not expand the dialog based on the label size. Here is how it looks:
How can I fix it? (Please also let me know if there is better way of doing this.)
I am using Qt5 in Linux.
Since you have not used a QLayout the QLabel will be displayed as large as you can, a possible request is to change the size of QDialog to the recommended size of QLabel with sizeHint():
#include <QApplication>
#include <QDialog>
#include <QLabel>
#include <QTimer>
void notify (int intTime=1000)
{
QDialog notify;
notify.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
notify.setWindowFlag(Qt::FramelessWindowHint);
QLabel *lbl = new QLabel(¬ify);
lbl->setText("This is a test This is a test This is a test This is a test This is a test This is a test This is a test");
QApplication::processEvents();
notify.resize(lbl->sizeHint());
QTimer::singleShot(intTime, ¬ify, SLOT(close()));
notify.exec();
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
notify(5000);
exit(0);
// return a.exec();
}
The other possible solution is to use a QLayout:
#include <QApplication>
#include <QDialog>
#include <QLabel>
#include <QTimer>
#include <QVBoxLayout>
void notify (int intTime=1000)
{
QDialog notify;
QVBoxLayout *lay = new QVBoxLayout(¬ify);
//notify.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
notify.setWindowFlag(Qt::FramelessWindowHint);
QLabel *lbl = new QLabel;
lay->addWidget(lbl);
lbl->setText("This is a test This is a test This is a test This is a test This is a test This is a test This is a test");
QApplication::processEvents();
QTimer::singleShot(intTime, ¬ify, SLOT(close()));
notify.exec();
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
notify(5000);
exit(0);
// return a.exec();
}
Basically, I want to know why the combination of text alignment flags and setPageSize doesn't end up with text centered in the display.
The following program does nearly exactly what I want, except that the text ends up centered only horizontally.
#include <QApplication>
#include <QMainWindow>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsTextItem>
#include <QTextDocument>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow *mainWindow = new QMainWindow(0, Qt::FramelessWindowHint);
QGraphicsView view;
view.setAlignment(Qt::AlignLeft | Qt::AlignBottom);
view.setFrameStyle(0);
view.setBackgroundBrush(QBrush(QColor(Qt::black)));
mainWindow->setCentralWidget(&view);
QGraphicsScene scene(0, 0, 640, 480);
QGraphicsTextItem textItem;
textItem.setTextWidth(640);
textItem.document()->setPageSize(QSizeF(640, 480));
textItem.document()->setDocumentMargin(0);
textItem.document()->setDefaultTextOption(QTextOption(Qt::AlignCenter | Qt::AlignVCenter));
textItem.setDefaultTextColor(QColor(Qt::white));
textItem.setFont(QFont("monospace", 18, 63));
textItem.setHtml("Center me!");
scene.addItem(&textItem);
textItem.setVisible(true);
view.setScene(&scene);
mainWindow->show();
return a.exec();
}
I should also note that this project is constrained to Qt 4.7.1.
How do I align text both horizontally and vertically in the center of a QGraphicsView using a QGraphicsTextItem? I'm fine with a stylesheet-based solution as well.
No, it doesn't.
This bug indicates that the QTextDocument (the underlying text rendering object) specifically does not attempt to vertical alignment. After looking about, most workarounds fall into two categories. For simple (i.e. single-line plain-text) applications, implement a proxy item that renders the text in its overloaded paint method as so:
class MyProxyGraphicsItem: public QGraphicsItem {
public:
explicit MyProxyGraphicsItem(QString text, QRectF geometry, QGraphicsItem *parent=0) :
QGraphicsItem(parent), text(text), geometry(geometry)
{}
virtual ~MyProxyGraphicsItem() {}
QRectF boundingRect() const { return geometry; }
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
painter->drawText(geometry, text, Qt::AlignCenter | Qt::AlignVCenter);
}
private:
QRectF geometry;
QString text;
}
For multi-line plain-text or rich-text (HTML), use a QLabel in stead as follows:
#include <QApplication>
#include <QMainWindow>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsTextItem>
#include <QLabel>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow *mainWindow = new QMainWindow(0, Qt::FramelessWindowHint);
QGraphicsView view;
view.setAlignment(Qt::AlignLeft | Qt::AlignBottom);
view.setFrameStyle(0);
view.setBackgroundBrush(QBrush(QColor(Qt::black)));
mainWindow->setCentralWidget(&view);
QGraphicsScene scene(0, 0, 640, 480);
QLabel label("<div style=\"color:white;\">Center me!</div>");
label.setWordWrap(true);
label.setAlignment(Qt::AlignCenter | Qt::AlignVCenter);
label.setFont(QFont("monospace", 18, 63));
scene.addWidget(&label)->setGeometry(QRectF(0,0,480,640));
view.setScene(&scene);
mainWindow->show();
return a.exec();
}
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);
}