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();
}
Related
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget w;
QPushButton *trankil = new QPushButton("Clique&&Release", &w);
QVBoxLayout *layoutrkl = new QVBoxLayout;
layoutrkl->addWidget(trankil);
//trankil->move(10,10);
int resultat = 2;
QLCDNumber *lcd = new QLCDNumber(&w);
QLabel *lelabel = new QLabel("bonsoir");
QPushButton *trankil2 = new QPushButton("Clique&&Release2", &w);
layoutrkl->addWidget(trankil2);
layoutrkl->addWidget(lelabel);
layoutrkl->addWidget(lcd);
QObject::connect(trankil, SIGNAL(clicked()),lcd, SLOT(display(resultat)));
w.setLayout(layoutrkl);
w.show();
return a.exec();
}
The connect doesn't work and i don't understand why at all !
There is no problem about how it appear, but if i click the QPushbutton, the QLCD won't display resultat
Thanks for your help
PS : There is my includes :
#include <QApplication>
#include <QVBoxLayout>
#include <QLCDNumber>
#include <QPushButton>
#include <QLabel>
#include <QObject>
If you debug it.
QObject::connect: No such slot QLCDNumber::display(resultat)
display(resultat) is not a slot function.
You can try the following:
QObject::connect(trankil, &QPushButton::clicked, lcd, [&](){
lcd->display(QString::number(resultat));
});
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);
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'm attempting to create a program to display notifications on my desktop. I've started by using a QLabel that pops up whenever I change my volume.
Here I have a function that takes a QLabel and string as parameters and updates the label with the string's text:
void displayNotif (QLabel* label, int labelText) {
labelStr = QString::number(labelText) + "% volume";
label -> setText(labelStr);
label -> raise();
label -> show();
//Animation
QPropertyAnimation *slideIn = new QPropertyAnimation(label, "pos");
slideIn->setDuration(750);
slideIn->setStartValue(QPoint(1800, 30));
slideIn->setEndValue(QPoint(1250, 30));
slideIn->setEasingCurve(QEasingCurve::InBack);
slideIn->start();
// Wait 3 seconds
QEventLoop loop;
QTimer::singleShot(3000, &loop, SLOT(quit()));
loop.exec();
// Close block
label -> hide();
}
This function is called in a loop in the main that waits every 1 second and checks if the volume has changed. My issue is that whenever I increase the volume over more than one second, the dialog ends up displaying twice (or more), which makes sense because it checks again and the volume is not the same as it was a second ago.
What I'd like to do is have the label update continuously for the three seconds that is showing, but as far as I know, you can just
while( loop.exec() ) { //UpdateLabel }
How can I accomplish this? It would also help to be able to then have it show for longer if the volume is still increasing/decreasing.
Thanks in advance!
Edit:
Here's what the main function, which calls the displayNotif, looks like:
#include <QApplication>
#include <QLabel>
#include <QProcess>
#include <QTimer>
#include "getBattery.h"
#include "getVolume.h"
#include "displayNotif.h"
#include "AnimatedLabel.h"
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
// Create Label
QLabel *hello = new QLabel();
int vol;
vol = getVolume();
QEventLoop loop;
while (true) {
//Check if volume is updated
if (getVolume() != vol) {
vol = getVolume();
displayNotif (hello, vol);
}
// Wait .2 second
QTimer::singleShot(200, &loop, SLOT(quit()));
loop.exec();
}
return app.exec();
}
It is not necessary to use while True for this repetitive task, just use a QTimer, when using QEventLoop you do not leave any way to update any component of the GUI.
#include <QApplication>
#include <QLabel>
#include <QTimer>
#include <QDebug>
#include <QPropertyAnimation>
class NotifyLabel: public QLabel{
Q_OBJECT
QTimer timer{this};
QPropertyAnimation slideIn{this, "pos"};
public:
NotifyLabel(){
timer.setSingleShot(true);
timer.setInterval(3000);
connect(&timer, &QTimer::timeout, this, &NotifyLabel::hide);
slideIn.setDuration(750);
slideIn.setStartValue(QPoint(1800, 30));
slideIn.setEndValue(QPoint(1250, 30));
slideIn.setEasingCurve(QEasingCurve::InBack);
}
void displayNotif(int value){
if(timer.isActive()){
timer.stop();
}
else
slideIn.start();
setText(QString("%1% volume").arg(value));
show();
timer.start();
}
};
static int getVolume(){
// emulate volume
return 1+ rand() % 3;
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
NotifyLabel w;
QTimer timer;
int current_vol;
QObject::connect(&timer, &QTimer::timeout, [&w, ¤t_vol](){
int update_vol = getVolume();
qDebug()<<update_vol;
if(current_vol != update_vol){
w.displayNotif(update_vol);
}
current_vol = update_vol;
});
timer.start(2000);
return a.exec();
}
#include "main.moc"
in the following link you will find the complete example.
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();
}