Im trying to simply display of 2 different QTextEdit value into a QLabel. I have tried for a single QTextEdit but couldn't display the value of both QTextEdit.
void MainWindow::on_pushButton_clicked()
{
ui->label_az->setText(ui->textEdit_ra1->toPlainText());
ui->label_az->setText(ui->textEdit_ra2->toPlainText());
}
It doesn't display the QTextEdit values when I click on pushbutton. Thank you in advance
Just to summarize our comments into a single post: QLabel::setText replaces the content of the label, so you have to create the whole string before and set it once. Code below will do it:
void MainWindow::on_pushButton_clicked()
{
ui->label_az->setText(
ui->textEdit_ra1->toPlainText() +
" " + // use here the separator you find more convenient
ui->textEdit_ra2->toPlainText());
}
The second setText() call replaces the label's text. You want to combine both texts into a single label text, like this:
label->setText(text_1->toPlainText() + "\n" + text_2->toPlainText());
Here's a complete example program, to give context:
#include <QWidget>
#include <QBoxLayout>
#include <QTextEdit>
#include <QPushButton>
#include <QLabel>
#include <QApplication>
#include <memory>
int main(int argc, char **argv)
{
QApplication app{argc, argv};
const auto w = std::make_unique<QWidget>();
const auto window = w.get();
const auto layout = new QVBoxLayout(window);
const auto text_1 = new QTextEdit(window);
layout->addWidget(text_1);
const auto text_2 = new QTextEdit(window);
layout->addWidget(text_2);
const auto button = new QPushButton("Push Me!", window);
layout->addWidget(button);
const auto label = new QLabel(window);
layout->addWidget(label);
QObject::connect(button, &QPushButton::pressed,
label, [=]() { label->setText(text_1->toPlainText() + "\n" + text_2->toPlainText()); });
window->show();
return app.exec();
}
Related
I am new working with QT. I am familiar with the QSlider class of Qt. But I am not sure whether it works with Timestamps. I have a QSlider object that can return an integer based on the position of its tick. However I want to customize it so that it can return timestamps (Ex. 10:50:20) instead.
Could you tell me whether its possible ? or how can I implement it?
My goal is to create a slider that has values between 0:00:00 to 23:59:59. And based on the position of the tick it can return a value between 0:00:00 and 23:59:59
okay! so it was really easy!
#include <QApplication>
#include <QSlider>
#include <QLabel>
#include <QTime>
#include <QVBoxLayout>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget w;
w.resize(500, 200);
w.show();
auto vLayout = new QVBoxLayout(&w);
auto slider = new QSlider(&w);
slider->setOrientation(Qt::Horizontal);
slider->setMinimum(0);
slider->setMaximum(10000);
auto timeLabel = new QLabel(&w);
timeLabel->setText("00:00:00");
vLayout->addWidget(slider,1);
vLayout->addWidget(timeLabel,1);
auto mapToRange = [](int minOutRange, int maxOutRange, int minInRange, int maxInRange, int value) ->double {
return (value-minInRange)/static_cast<double>(maxInRange-minInRange)*(maxOutRange-minOutRange)+minOutRange;
};
QObject::connect(slider, &QSlider::valueChanged, timeLabel, [timeLabel, slider, mapToRange](int value) ->void {
const QTime t = QTime::fromMSecsSinceStartOfDay(mapToRange(0,86399999, slider->minimum(), slider->maximum(), value));
timeLabel->setText(t.toString("hh:mm:ss"));
});
return a.exec();
}
I have a QLabel in a QVBoxLayout. Most of the times, it only has one line of text, but sometimes, the text can be too long to fit in one line. So I have to enable wordWrap.
I want the label to be as (vertically) small as possible, thus I set setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Maximum).
Now, if there's enough vertical space, the label is higher as it would have to be with only one line:
At the same window size and without wordWrap being enabled, the label only takes the minimum space I would like it to take:
Can this also be achieved with wordWrap being enabled and independent of the window height?
I tried to reproduce the behavior with a small example. Maybe this might help you to solve your issue. Just enlarge the widget and type some random text having several words separated by white spaces.
The idea is to use the correct combination of QSizePolicys not just for the QLabel, but also for the other GUI elements.
#include <QFrame>
#include <QLabel>
#include <QGroupBox>
#include <QLineEdit>
#include <QHBoxLayout>
#include <QPushButton>
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
auto frame = new QFrame;
frame->setLayout(new QVBoxLayout);
auto groupEdit = new QGroupBox;
groupEdit->setLayout(new QHBoxLayout);
auto edit = new QLineEdit;
groupEdit->layout()->addWidget(edit);
frame->layout()->addWidget(groupEdit);
auto group = new QGroupBox;
frame->layout()->addWidget(group);
group->setLayout(new QHBoxLayout);
auto label = new QLabel;
groupEdit->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
group->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
group->layout()->addWidget(label);
group->layout()->addWidget(new QPushButton);
QObject::connect(edit, &QLineEdit::textEdited, [&](const QString& text) {
label->setText(text);
label->setWordWrap(true);
});
frame->show();
return a.exec();
}
Today I was trying to take a picture of the QSlider handle in order to use it in an adpated QSlider widget with two handles.
This is somewhat similar to the following question: Range slider in Qt (two handles in a QSlider)
For a full fledged solution I can not use a simple image files. I tried to create the image of the QSlider by using the drawComplexControl function, but it leaves me basically with a black image.
What I'm doing wrong here? It seems so simple to me, but it is just not working.
#include <QApplication>
#include <QPushButton>
#include <QPainter>
#include <QStyleOptionSlider>
int main(int argc, char** args) {
QApplication app(argc, args);
auto slider = new QSlider;
slider->setOrientation(Qt::Orientation::Horizontal);
slider->show();
auto btn = new QPushButton("Create Image");
QObject::connect(btn, &QPushButton::clicked, [&] {
auto style = QApplication::style();
QStyleOptionSlider sliderOptions;
QPixmap pix(slider->size());
auto painter = new QPainter();
painter->begin(&pix);
style->drawComplexControl(QStyle::CC_Slider, &sliderOptions, painter, slider);
pix.save("SliderImage.png");
auto handleRect = style->subControlRect(QStyle::ComplexControl::CC_Slider, &sliderOptions, QStyle::SubControl::SC_SliderHandle, slider);
QPixmap handlePix = pix.copy(handleRect);
handlePix.save("SliderHandleImage.png");
painter->end();
});
btn->show();
app.exec();
}
The solution was very simple. I just forgot to add:
sliderOption.initFrom(slider);
What is the procedure to make a text being written in a QLineEdit widget, dynamically display inside a QTextEdit that already contains some text?
For example, let us say that a QLineEdit asks for a name where one writes "John". Is it possible to display it in real time inside a QTextEdit containing :
The name is + textFromQLineEdit + , age 24 ?
The displayed text has to dynamically take into account the changes being made to the QLineEdit so that the user does not need to press a button or press enter to see his/her name appear.
The following is the minimal code for connecting the two widgets to each other using the signal textChanged() from QLineEdit and the slot setText() from QTextEdit (which does not allow for adding some text before and after the text from the QLineEdit) :
#include <QLineEdit>
#include <QVBoxLayout>
#include <QGroupBox>
#include <QTextEdit>
#include <QApplication>
class SmallWindow : public QWidget
{
Q_OBJECT
public:
SmallWindow();
private:
QLineEdit *nameLine;
QTextEdit *textBox;
};
SmallWindow::SmallWindow() : QWidget()
{
setFixedSize(300,250);
QLineEdit *nameLine = new QLineEdit;
QTextEdit *textBox = new QTextEdit;
QWidget::connect(nameLine,SIGNAL(textChanged(QString)),textBox,SLOT(setText(QString)));
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(nameLine);
layout->addWidget(textBox);
QGroupBox *group = new QGroupBox(this);
group->setLayout(layout);
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
SmallWindow window;
window.show();
app.exec();
}
#include "main.moc"
What should be done to keep the text before and after the QLineEdit text in place and updating the QTextEdit box in real time?
Create special slot:
void SmallWindow::pasteText(const QString& str)
{
textBox->setText(QString("The name is %1 , age 24").arg(str));
}
and don't use textChanged() signal because you need only one name accepted by user, so you need QLineEdit::editingFinished() (or maybe QLineEdit::returnPressed(), it depends on your needs)
connect(nameLine,SIGNAL(editingFinished(QString)),this,SLOT(pasteText(QString)));
Also you don't need QWidget::connect, because you write this code inside QObject subclass, so it is not necessary.
Also these lines:
QLineEdit *nameLine = new QLineEdit;
QTextEdit *textBox = new QTextEdit;
should be:
nameLine = new QLineEdit;
textBox = new QTextEdit;
Create an own slot for the text update. I think that you have also some errors in your code.
Widgets nameLine and textBox are already defined in the SmallWindow.h. You probably want to create them in SmallWindow.cpp following way:
nameLine = new QLineEdit;
textBox = new QTextEdit;
Also GroupBox group is not set to any layouts. Perhaps you want to create one layout more and set the widget there?
QVBoxLayout *mainlayout = new QVBoxLayout;
mainlayout->addWidget(group);
this->setLayout(mainlayout);
If you create an own slot for the text update, you can just change text content of the textBox there:
SmallWindow.h
#ifndef SMALLWINDOW_H
#define SMALLWINDOW_H
#include <QLineEdit>
#include <QVBoxLayout>
#include <QGroupBox>
#include <QTextEdit>
class SmallWindow : public QWidget
{
Q_OBJECT
public:
SmallWindow();
private slots:
void updateLineEditText(QString name);
private:
QLineEdit *nameLine;
QTextEdit *textBox;
};
#endif // SMALLWINDOW_H
SmallWindow.cpp
#include "SmallWindow.h"
SmallWindow::SmallWindow() : QWidget()
{
setFixedSize(300,250);
nameLine = new QLineEdit;
textBox = new QTextEdit;
connect(nameLine,SIGNAL(textChanged(QString)),this,
SLOT(updateLineEditText(QString)));
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(nameLine);
layout->addWidget(textBox);
QGroupBox *group = new QGroupBox(this);
group->setLayout(layout);
QVBoxLayout *mainlayout = new QVBoxLayout;
mainlayout->addWidget(group);
this->setLayout(mainlayout);
}
void SmallWindow::updateLineEditText(QString name) {
QString textEditString("The name is ");
textEditString.append(name);
textEditString.append(", age 24 ?");
textBox->setText(textEditString);
}
This is a minimal example in Qt 5, using C++11. It is about as concise as it would be in Python. If you are using Qt 5, then your question should have looked exactly as it does below, save for the connect statement. This is what "minimal" means when it comes to Qt. Avoid the fluff and boilerplate that doesn't add to the problem. There's no need to have a separate class for the window in such a simple example.
#include <QVBoxLayout>
#include <QLineEdit>
#include <QTextEdit>
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget window;
QVBoxLayout layout(&window);
QLineEdit name;
QTextEdit text;
layout.addWidget(&name);
layout.addWidget(&text);
QObject::connect(&name, &QLineEdit::textChanged, [&](const QString & name){
text.setPlainText(QString("The name is %1, age 24.").arg(name));
});
window.show();
return app.exec();
}
The same in Qt 4 is below - note the absence of any manual memory management. That's how your question ideally should have looked for Qt 4, without the slot's implementation of course. You can use the connectSlotsByName or an explicit connect, of course - that's just a matter of style.
#include <QVBoxLayout>
#include <QLineEdit>
#include <QTextEdit>
#include <QApplication>
class Window : public QWidget {
Q_OBJECT
QVBoxLayout m_layout; // not a pointer!
QLineEdit m_name; // not a pointer, must come after the layout!
QTextEdit m_text;
Q_SLOT void on_name_textChanged(const QString & name) {
m_text.setPlainText(QString("The name is %1, age 24.").arg(name));
}
public:
Window() : m_layout(this) {
m_layout.addWidget(&m_name);
m_layout.addWidget(&m_text);
m_name.setObjectName("name");
QMetaObject::connectSlotsByName(this);
}
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Window window;
window.show();
return app.exec();
}
#include "main.moc"
I have a QPushButton, this button has a text on it, this text is a number. In a slot of another object i want to change number that displayed on button, but when i call
MyButton->setText(QString("%1").arg(Number));
or
QString tmp;
tmp.setNum(Number);
MyButton->setText(tmp);
text on button dosen't changes.
But when i call
MyButton->setText("some random text");
it works fine.
How i can change number that displaying on button?
Part of my code:
sortWindow::sortWindow(QWidget *parrent)
{
...
MyButton = new QPushButton;
QString tmp(QString("%1").arg(Number));
MyButton.setText(tmp);
...
}
and
void sortWindow::workOnSignal(int index)
{
...
if (something)
{
...
QString tmp;
tmp.setNum(Number);
MyButton->setText(tmp);
...
}
Type of Number must be int. So it will work properly.
#include <QApplication>
#include <QPushButton>
int main(int argc,char **argv)
{
QApplication app(argc,argv);
QPushButton *pd = new QPushButton;
pd->setText(QString("%1").arg(1234));
pd->show();
return app.exec();
}