I want to draw a line to connect two circles (QGraphicsEllipseItem), but I find that I don't get the desired result with this way of writing.
//they have been initialized to the correct place
QGraphicsEllipseItem* nodeu;
QGraphicsEllipseItem* nodev;
this->addLine(nodeu->x(), nodeu->y(), nodev->x(), nodev->y());
The result of executing these codes is that only two circles appear, but no lines appear.
like this
My rough inference is the problem of coordinate transformation, but I just can't solve it.
thank you!
you should first add one QGraphicsView in your UI or :
QGraphicsView *graphicsView;
QGridLayout *gridLayout;
gridLayout = new QGridLayout(centralwidget);
gridLayout->setSpacing(0);
gridLayout->setObjectName(QString::fromUtf8("gridLayout"));
graphicsView = new QGraphicsView(centralwidget);
graphicsView->setObjectName(QString::fromUtf8("graphicsView"));
gridLayout->addWidget(graphicsView, 0, 0, 1, 1);
then :
QGraphicsScene *_scene = new QGraphicsScene(this);
ui->graphicsView->setScene(_scene);
ui->graphicsView->setRenderHints(QPainter::Antialiasing);
QGraphicsEllipseItem *nodeu = new QGraphicsEllipseItem;
nodeu->setRect(20, 10, 20, 20);
_scene->addItem(nodeu);
QGraphicsEllipseItem *nodev = new QGraphicsEllipseItem;
nodev->setRect(80, 60, 20, 20);
_scene->addItem(nodev);
QGraphicsLineItem *_lineItem = new QGraphicsLineItem;
_lineItem->setLine(nodeu->rect().x() + nodeu->rect().width() / 2.0, nodeu->rect().y() + nodeu->rect().height() / 2.0,
nodev->rect().x() + nodev->rect().width() / 2.0, nodev->rect().y() + nodev->rect().height() / 2.0);
_scene->addItem(_lineItem);
this is the output:
Related
I am trying to create a timeline app for visualization of certain time-sensitive requirements information and have begun experimentation with Qt since I am new to it. The problem I am having is that the rendered object will show up if I render it just basically on the main background, but when I tried to add the TimeLine objects to the vertical layout I have on my UI they refuse to render.
Here is my main window code for creating the objects:
void MainWindow::drawTimelines()
{
for(int i=0; i <= 3; i++)
{
TimeLine *tl = new TimeLine(this, this);
// ui->verticalLayout->addWidget(tl, 0, Qt::AlignLeft);
tl->lower();
QPoint * dest = new QPoint(this->width() - (this->width() / 12), i * this->height() / 4 + this->height() / 4);
QPoint * src = new QPoint(this->width() / 12, i * this->height() / 4 + this->height() / 4);
// tl->setGeometry(0, 0, this->width(), 100);
tl->setGeometry(src->x(), src->y(), this->width(), 100);
tl->updateGeometry();
QPoint *startPt = new QPoint(src->x(), 50);
QPoint *endPt = new QPoint(dest->x(), 50);
// QPoint *src = new QPoint(this->width() /8, 50);
// QPoint *dest = new QPoint(this->width() - (this->width() / 8), 50);
tl->setSrcPt(startPt);
tl->setDestPt(endPt);
tl->setNumSegments(3);
timeLineList.push_back(tl);
timeLineList.at(i)->show();
}
update();
}
Here is the maint function present in the TimeLine object itself:
void TimeLine::paintEvent(QPaintEvent * event)
{
QRectF frame(QPointF(sourcePoint->x(), sourcePoint->y()), geometry().size());
QPainter painter(this);
painter.setPen(QPen(Qt::FlatCap));
painter.drawRoundedRect(frame, 10.0, 10.0);
int translateAmount = sourcePoint->y() - window->getPainterY();
painter.translate(0, translateAmount);
// window->setPainterY(translateAmount);
painter.drawLine(sourcePoint->x(), 25, destPoint->x(), 25);
for(int i = 0; i <= numSegments; i++){
int xPoint = ((destPoint->x() - sourcePoint->x()) * i / numSegments) + sourcePoint->x();
int yPoint = 25;
painter.drawLine(xPoint, yPoint + 20, xPoint, yPoint - 20);
}
QWidget::paintEvent(event);
}
For reference, the verticalLayout is meant to have one
The TimeLines (and bounded rect boxes) do not render when I have the
// ui->verticalLayout->addWidget(tl, 0, Qt::AlignLeft);
uncommented. As you can see from a bunch of the other commented lines, I have tried numerous other things to try and render these TimeLines. I have tried:
changing reference points of the geometry to be (0,0) for the new segment of the vertical layout
changing the size of the geometry
both translating and not translating the painter
changing line thickness, type of line, etc.
even tried rendering something else simple in the vertical layout
The part that confuses me is that even the bounded rect made based on the geometry of the TimeLine frame gets cut off on the side and top of the timeline (it only shows top left corner and the top and left side-lines) even when rendered on the normal screen.
I'm trying to use QtCharts with X-Axis and Y-Axis in the center of chart.
Therefore the center point will locate in the middle of my QtChart... There are options to alignment axis to the top/bottom or left/right (addAxis(QValueAxis*,Qt::AlignTop)). But, there is no option to align it to the center. When I use AlignCenter as addAxis argument, I get an error.
Any Help?
Tanks.
Edit (Providing code and error):
QChart *BScopeChart = new QChart();
QLineSeries *BScopeSerie = new QLineSeries(this);
BScopeChart->setMargins(QMargins(0, 0, 0, 0));
QValueAxis *BScopeAxisX = new QValueAxis;
BScopeAxisX->setRange(-50, 50);
QValueAxis *BScopeAxisY = new QValueAxis;
BScopeAxisY->setRange(-5, 5);
BScopeChart->addAxis(BScopeAxisY, Qt::AlignLeft); // I need this Line change to something like: BScopeChart->addAxis(BScopeAxisY, Qt::AlignCenter)
BScopeChart->addAxis(BScopeAxisX, Qt::AlignBottom); // I need this Line change to something like: BScopeChart->addAxis(BScopeAxisX, Qt::AlignCenter);
ui.widgetBScopeQChart->setChart(BScopeChart);
and when I change those two lines to:
BScopeChart->addAxis(BScopeAxisY, Qt::AlignCenter);
BScopeChart->addAxis(BScopeAxisX, Qt::AlignCenter);
I face this error:
ErrorScreenshot
I'm developing a file dialog to import file in my application and I want to have an additional QComboBox with a list of formats between File names edit and File of types filter combo box, like this:
I've managed to add QComboBox under filters like this:
using this code:
QGridLayout * layout = qobject_cast <QGridLayout *>(dialog->layout());
QLabel * labelFormat = new QLabel(tr("Format"), dialog);
layout->addWidget(labelFormat, 4, 0);
QComboBox * comboBoxFormat = new QComboBox(dialog);
layout->addWidget(comboBoxFormat, 4, 1);
But I need to swap the last two rows of this grid layout. I've tried something like this to swap rows:
QWidget * w0 = layout->itemAtPosition(3, 0)->widget();
QWidget * w1 = layout->itemAtPosition(3, 1)->widget();
QWidget * w2 = layout->itemAtPosition(3, 2)->widget();
QLabel * labelFormat = new QLabel(tr("Format"), dialog);
layout->addWidget(labelFormat, 3, 0);
QComboBox * comboBoxFormat = new QComboBox(dialog);
layout->addWidget(comboBoxFormat, 3, 1);
layout->replaceWidget(w0, labelFormat);
layout->replaceWidget(w1, comboBoxFormat);
layout->addWidget(w0, 4, 0);
layout->addWidget(w1, 4, 1);
layout->addWidget(w2, 4, 2);
But I got the wrong widgets position:
How can I achieve the widget positioning from the first screenshot?
In your case the problem is caused by the fact that you are incorrectly locating QDialogButtonBox, this must be in position 3, 2 occupying 2 rows and 1 column:
QGridLayout *layout = qobject_cast<QGridLayout *>(dialog->layout());
QWidget * w0 = layout->itemAtPosition(3, 0)->widget();
QWidget * w1 = layout->itemAtPosition(3, 1)->widget();
QWidget * w2 = layout->itemAtPosition(3, 2)->widget();
QLabel * labelFormat = new QLabel("Format", dialog);
layout->addWidget(labelFormat, 3, 0);
QComboBox * comboBoxFormat = new QComboBox(dialog);
layout->addWidget(comboBoxFormat, 3, 1);
layout->replaceWidget(w0, labelFormat);
layout->replaceWidget(w1, comboBoxFormat);
layout->addWidget(w0, 4, 0);
layout->addWidget(w1, 4, 1);
layout->addWidget(w2, 3, 2, 2, 1);
I have a QMessageBox which I'd like it to be bigger. It's a simple QMessageBox with two standard buttons, Ok and Cancel. The problem is that it is very small for my application's purposes. Code shows like this:
QMessageBox msg;
msg.setText("Whatever");
msg.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
msg.setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
int ret = msg.exec();
switch (ret) {
case QMessageBox::Ok:
ui->textEdit->clear();
break;
case QMessageBox::Cancel:
break;}
I tried several ways to increase the size:
msg.setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
msg.setSizePolicy(QSizePolicy::Maximum,QSizePolicy::Maximum);
msg.setFixedHeight(600);
msg.setFixedWidth(600);
I even cleared and rebuilt, and it compiles everything but nothing take effect...
Do you have any idea on how to set QMessageBox size "by hand"? Thanks.
You can edit the css of the label:
msg.setStyleSheet("QLabel{min-width: 700px;}");
You can similarly edit the css of the buttons to add a margin or make them bigger.
For example:
msg.setStyleSheet("QLabel{min-width:500 px; font-size: 24px;} QPushButton{ width:250px; font-size: 18px; }");
There is also a trick mentioned:
QSpacerItem* horizontalSpacer = new QSpacerItem(800, 0, QSizePolicy::Minimum, QSizePolicy::Expanding);
QGridLayout* layout = (QGridLayout*)msg.layout();
layout->addItem(horizontalSpacer, layout->rowCount(), 0, 1, layout->columnCount());
But this doesn't seem to work for everyone.
coyotte508's answer caused my layout to be horribly off center and at different widths it was cut off. In searching around further I found this thread which explains a better solution.
In essence the layout of a messagebox is a grid, so you can add a SpacerItem to it to control the width. Here's the c++ code sample from that link:
QMessageBox msgBox;
QSpacerItem* horizontalSpacer = new QSpacerItem(500, 0, QSizePolicy::Minimum, QSizePolicy::Expanding);
msgBox.setText( "SomText" );
QGridLayout* layout = (QGridLayout*)msgBox.layout();
layout->addItem(horizontalSpacer, layout->rowCount(), 0, 1, layout->columnCount());
msgBox.exec();
You can subclass QMessageBox and reimplement resize event handler as following:
void MyMessageBox::resizeEvent(QResizeEvent *Event)
{
QMessageBox::resizeEvent(Event);
this->setFixedWidth(myFixedWidth);
this->setFixedHeight(myFixedHeight);
}
I wanted my QMessageBox width to adapt in proportion to the length of the text content with a certain amount of buffer to avoid line wrap. After surveying numerous forums and threads including this one, I came up with:
int x_offset = (2.0 * MainWindow::geometry().x());
int y_offset = (0.5 * MainWindow::geometry().y());
msgBox.setText(vers_msg.data());
QSpacerItem* horizontalSpacer = new QSpacerItem
(8 * vers_msg.size(), 0,
QSizePolicy::Minimum, QSizePolicy::Expanding);
QGridLayout* layout = (QGridLayout*)msgBox.layout();
layout->addItem(horizontalSpacer, layout->rowCount(),
0, 1, layout->columnCount());
msgBox.setGeometry(
MainWindow::geometry().x() + x_offset,
MainWindow::geometry().y() + y_offset,
msgBox.geometry().width(),
msgBox.geometry().height());
Adjust the hard numbers in x_offset, y_offset and horizontalSpacer to suit your situation. I was hoping it would be easier than this but at least this works.
Inspired by the "inspect the Qt source code and adapt" approach, this worked for me with Qt 5.12:
if (auto grid = dynamic_cast<QGridLayout*>(msgBox.layout())) {
if (auto text = grid->itemAtPosition(0, grid->columnCount() - 1); text && text->widget()) {
text->widget()->setFixedWidth(500);
}
}
Keep in mind, of course, that this will break if ever they change the way they do layouts of QMessageBox.
Hi guys I have to dynamically create push buttons depending on user inputs, therefore if user gives a large input number the widget containing the push buttons has to have the ability to scroll up and down. For this reason I am using QScrollArea. I generate the template in Qt designer and the UIC generates the code for me after which I add in my part which should handle dynamic creation of push buttons. However, I can not seem to get the vertical scroll bars to appear. Here is the relevant part of the code.
verticalWidget = new QWidget(FWHMWorkflowDialog);
verticalWidget->setObjectName(QString::fromUtf8("verticalWidget"));
verticalWidget->setMinimumSize(QSize(150, 0));
verticalWidget->setMaximumSize(QSize(150, 16777215));
verticalLayout_5 = new QVBoxLayout(verticalWidget);
verticalLayout_5->setObjectName(QString::fromUtf8("verticalLayout_5"));
scrollArea = new QScrollArea(verticalWidget);
scrollArea->setObjectName(QString::fromUtf8("scrollArea"));
scrollArea->setMaximumSize(QSize(150, 16777215));
scrollArea->setWidgetResizable(true);
scrollAreaWidgetContents = new QWidget();
scrollAreaWidgetContents->setObjectName(QString::fromUtf8("scrollAreaWidgetContents"));
scrollAreaWidgetContents->setGeometry(QRect(0, 0, 130, 432));
numberOfSlices = numberSlices;
for (int i = 0; i < numberOfSlices; i++)
{
QWidget *horizontalWidget = new QWidget(scrollAreaWidgetContents);
horizontalWidget->setMaximumSize(150,40);
horizontalWidget->setGeometry(QRect(0, i*40, 150, 40));
hWidgetList.push_back(horizontalWidget);
QHBoxLayout *hLayout = new QHBoxLayout(horizontalWidget);
hLayoutList.push_back(hLayout);
hLayout->setSizeConstraint(QLayout::SetMinimumSize);
hLayout->setContentsMargins(-1, 1, -1, 1);
QPushButton *pushButton = new QPushButton(horizontalWidget);
pushButtonList.push_back(pushButton);
QString temp = QString("m_sliceButton").arg(i);
pushButtonList[i]->setObjectName(temp);
pushButtonList[i]->setGeometry(QRect(10, 20+i*40, 98, 27));
hLayout->addWidget(pushButton);
QCheckBox *checkBox = new QCheckBox(horizontalWidget);
checkBoxList.push_back(checkBox);
temp = QString("m_checkBox").arg(i);
checkBoxList[i]->setObjectName(temp);
checkBoxList[i]->setEnabled(true);
checkBoxList[i]->setGeometry(QRect(110, 20+i*40, 21, 22));
hLayout->addWidget(checkBox);
}
scrollArea->setWidget(scrollAreaWidgetContents);
//scrollArea->setWidgetResizable(true);
verticalLayout_5->addWidget(scrollArea);
The output window always looks like the following.
In this example the input by the user is 25 however you can see that the 21st button is cut off and 4 other buttons are not visible.
The size window problem occurring after scroll functionality started working.
You need to add your horizontalWidget to a vertical widget like so:
QVBoxLayout* vLayout = new QVBoxLayout();
for (int i = 0; i < numberOfSlices; i++)
{
QWidget *horizontalWidget = new QWidget();
vLayout->addWidget(horizontalWidget);
....
}
scrollAreaWidgetContents->setLayout(vLayout);
You second problem looks like it comes from this line:
scrollArea = new QScrollArea(verticalWidget);
You're adding scrollArea directly to verticalWidget, but to get it to lay out the way you want you need to put it in a layout. Try the following instead:
QVBoxLayout* l = new QVBoxLayout();
l->addWidget(sliceLabel); // or whatever you call it
l->addWidget(scrollArea);
l->addWidget(clearButton); // again, your name here
verticalWidget->setLayout(l);
Try playing around with the QScrollBarPolicy.
http://doc.qt.digia.com/qt/qabstractscrollarea.html#horizontalScrollBarPolicy-prop
I'm guessing that the default behavior isn't working because there is something strange going on with layouts.