Qt::AlignVCenter does not work on QTextTable - qt

I created QTextTable:
QTextDocument *document=new QTextDocument(this);
QTextCursor cursor(document);
cursor.movePosition(QTextCursor::Start);
QTextTableCellFormat cellFormat;
cellFormat.setLeftPadding(7);
cellFormat.setRightPadding(7);
QBrush blackBrush(Qt::SolidPattern);
QTextTableFormat tableFormat;
tableFormat.setAlignment(Qt::AlignCenter);
tableFormat.setBorderBrush(blackBrush);
tableFormat.setBorder(0.5);
tableFormat.setCellSpacing(0);
tableFormat.setBorderStyle(QTextFrameFormat::BorderStyle_Solid);
tableFormat.setWidth(QTextLength(QTextLength::PercentageLength, 100));
QTextTable *table = cursor.insertTable(10,10,tableFormat);
QTextBlockFormat centerAlignment;
centerAlignment.setAlignment(Qt::AlignCenter);
table->mergeCells(0,0,10,5);
cursor = table->cellAt(0, 0).firstCursorPosition();
cursor.setBlockFormat(centerAlignment);
cursor.insertText("text");
I want to write text in the middle of cell vertically and horizontally
But my text is in the middle horizontally but it is not in the middle vertically.
Also Qt::AlignVCenter and Qt::AlignBottom do not work.
I tried this answer https://stackoverflow.com/a/10329809/1997790 but it is not worked.

You might try using QTextCharFormat::setVerticalAlignment(), as in:
QTextTableCell cell = table->cellAt(0, 0);
QTextCharFormat cellFormat = cell.format();
cellFormat.setVerticalAlignment(QTextCharFormat::AlignMiddle);
cell.setFormat(cellFormat);
If that doesn't work, you might want to try using style sheets to customize the table.

Related

Adding label to video

I have to write a simple video player that can display some subtitles,link or a picture(like on YouTube) in a certain time. I have no idea of how do display anything using QVideoWidget. I couldn't find any useful class to do it. Could you please give me some advices?
I did It your way but after i load any video QLabel disappears...
player->setVideoOutput(vw);
playlistView->setMaximumWidth(200);
playlistView->setMinimumWidth(300);
window = new QWidget;
Playerlayout = new QGridLayout;
subtitleWidget = new QLabel;
subtitleWidget->setMaximumWidth(1000);
subtitleWidget->setMaximumHeight(100);
subtitleWidget->setStyleSheet("QLabel {background-color : red; color
blue;}");
subtitleWidget->setAlignment(Qt::AlignCenter | Qt::AlignBottom);
subtitleWidget->setWordWrap(true);
subtitleWidget->setText("example subtitle");
Playerlayout->addWidget(vw,0,0);
Playerlayout->addWidget(subtitleWidget,0,0);
Playerlayout->addWidget(playlistView,0,1,1,2);
If QVideoWidget doesn't provide what you require directly then you could always set up an overlay.
The basic layout item hierarchy would be something like...
QWidget
layout
QVideoWidget
subtitle_widget
In this case the layout could be either a QStackedLayout using stacking mode QStackedLayout::StackAll or a QGridLayout with both the QVideoWidget and the subtitle_widget occupying the same cells but with the correct z-order.
Going with the QGridLayout...
auto *w = new QWidget;
auto *l = new QGridLayout(w);
auto *video_widget = new QVideoWidget;
auto *subtitle_widget = new QLabel;
/*
* Subtitles will be shown at the bottom of the 'screen'
* and centred horizontally.
*/
subtitle_widget->setAlignment(Qt::AlignHCenter | Qt::AlignBottom);
subtitle_widget->setWordWrap(true);
/*
* Place both the video and subtitle widgets in cell (0, 0).
*/
l->addWidget(video_widget, 0, 0);
l->addWidget(subtitle_widget, 0, 0);
Subtitles etc. can now be displayed simply by invoking subtitle_widget->setText(...) at the appropriate time.
The same method can easily be extended to overlaying other types of information.

How to remove margin from QChartView or QChart

All Qt Charts seem to have a margin and rounded corners.
How to remove both?
I want the white area to fill the entire dialog. I cannot find any informations in the Qt documentations. I found a screenshot of one Example that does not have this spacing. But I cannot find the code that disables it.
My initialization code looks like this:
QPieSeries *series = new QPieSeries();
series->append("Jane", 1);
series->append("Joe", 2);
series->append("Andy", 3);
series->append("Barbara", 4);
series->append("Axel", 5);
QChart *chart = new QChart();
chart->addSeries(series);
QChartView *chartView = new QChartView(chart);
chartView->setBackgroundBrush(Qt::red);
chartView->setRenderHint(QPainter::Antialiasing);
QMainWindow window;
window.setCentralWidget(chartView);
window.resize(400, 300);
window.show();
Devopia answerd the question in the comments!
In my example above I needed the following 2 lines of code to remove the red part completely:
chart->layout()->setContentsMargins(0, 0, 0, 0);
chart->setBackgroundRoundness(0);
At Qt 5.11 this does not work because the layout() method const (inherited from QGraphicsWidget):
QGraphicsLayout *QGraphicsWidget::layout() const
Furthermore it is not possible to take a copy of this because QGraphicsLayout is a base class.
For me this works:
chart->setMargins(QMargins(0,0,0,0));

How can I remove spacing around a widget in a layout?

I am trying to insert a very tiny QLabel into a very tiny QFrame.
The QFrame is used as a spacer and I thought it a perfect place to add text.
In code that text would change (in to mm). Because mm is so giant it is clipped...
Yet it seems to me it would fit if I could just get rid of the margins !
So I try:
ui->tinyFrame is created in Designer... it has geometry 0,0,10,10
QLabel* unitLabel = new QLabel("mm");
unitLabel->setFixedSize(8,8);
unitLabel->setFont(QFont("Arial Narrow", 7));
unitLabel->setMargin(0);
unitLabel->setContentsMargins(0,0,0,0);
QHBoxLayout* unitLayout = new QHBoxLayout();
unitLayout->setSpacing(0); // I try everything
unitLayout->setMargin(0);
unitLayout->setContentsMargins(0,0,0,0);
ui->tinyFrame->setLayout(unitLayout);
ui->tinyFrame->setContentsMargins(0,0,0,0);
unitLayout->addWidget(unitLabel);
What else can I try to remove the space around my little label ?
I am trying to insert a very tiny QLabel into a very tiny QFrame.
What can I try to remove the space around my little label?
QLabel is derived from QFrame: maybe you can just replace that QFrame with QLabel then? And no nested layout for that frame.
... as long as you tried layout->setSpacing(0) already...

how to get the end position of drawItemText in Qt

I am trying to customize a QPUshButton by subclassing it and overriding the paintEvent. I am writing the text, followed by the icon as below:
paintEvent(QPaintEvent *paint)
{
QStyleOption opt;
opt.init(this);
QPainter p(this);
//Draw the base
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
//Draw the text
style()->drawItemText(&p,this->rect(),Qt::AlignCenter,(this->palette()), true, this->text());
//How do I make the image immediately follow the text
if(!this->icon().isNull())
//Draw the icon at 75% button height
style()->drawItemPixmap(&p, this->rect(),Qt::AlignRight|Qt::AlignVCenter, this->icon().pixmap(this->rect().height() * 0.75));
}
I am center aligning the text, and right aligning the icon. However, this causes a gap between the text and the icon. Is there any way for me to draw the icon immediately after the text, instead of aligning?
In other words, is there any way to get the position where the drawItemText finished?
QStyle::itemTextRect() will tell you where the text will be laid down with given rectangle, font metrics and alignment.
QFontMetrics will tell you how wide your text is. It doesn't know about your rectangle, so you will have to do the alignment calculation yourself.

How to add an image on the top of another image?

I want to show difference between a trimed clip and non trimed clip in my video editor application, i.e. I want to add a small film image on my thumbnail for a trimed clip. How can I do this?
It would be just to show the difference between an image and a video in our gallery application.
How to add an image on the top of another one in Qt?
Open the QPainter on the bottom image and draw the top image using its drawPixmap()/drawImage() methods.
QPixmap base, overlay; // come from your code
{
QPainter painter(base);
painter.drawPixmap(100, 100, overlay);
}
If your overlay contains an alpha channel (e.g. fancy PNG icon) and your base image does not, you should create a new QPixmap with an alpha channel and draw both images into it:
QPixmap base, overlay; // come from your code
QPixmap result(base.width(), base.height());
result.fill(Qt::transparent); // force alpha channel
{
QPainter painter(&result);
painter.drawPixmap(0, 0, base);
painter.drawPixmap(100, 100, overlay);
}
QPixmaps and QImages can be used interchangeably, although not all combinations give good performance).
If it's just about showing an image above another, then you could also go with this answer.
QGridLayout *layout = new QGridLayout(widget);
Pixmap base, overlay;
QLabel *background = new Label();
background->setPixmap(&base);
QLabel *lOverlay = new QLabel();
lOverlay->setPixmap(&overlay);
//label gets positioned above textBrowser and is an overlay
layout->addWidget(background, 0, 0, Qt::AlignLeft | Qt::AlignTop);
layout->addWidget(lOverlay, 0, 0, Qt::AlignRight | Qt::AlignBottom);
Of course then the QPixbuf of the background doesn't contain the QPixbuf of the overlay-image, but it only appears to do.

Resources