I have an icon that is saved in a format:
//icon.h
extern const unsigned char icon[];
//icon.cpp
const unsigned char icon[]={0x17,0x3f,0x0c,....,0x10,0x06}
Now i want to add this icon to the status bar.
How do i do it?
Thank you.
First create a widget that loads the icon data, like a QLabel on which you set a QPixmap. What format is that image in? You will have to load it into your pixmap using one of the constructors, or you could try loading it with loadFromData().
Then add that widget to the status bar like so:
statusBar()->addWidget(yourIconWidget);
Have a look at statusBar(), addWidget() and addPermanentWidget().
An example of how to create the widget could be:
QPixmap *pixmap = new QPixmap;
// Note that here I don't specify the format to make it try to autodetect it,
// but you can specify this if you want to.
pixmap->loadFromData(icon, sizeof(icon) / sizeof(unsigned char));
QLabel *iconLbl = new QLabel;
iconLbl->setPixmap(pix);
statusBar()->addWidget(iconLbl);
Specifying the format, as I mentioned above, is elaborated upon here.
Related
I want to display a lot of images in my Qt application, for that I have created a button that when clicked will access to the computer's user and add images. My issue is that I don't know how to display these images in the application.
Here is my Code:
void Mainwindow::on_pushButton_pressed()
{
QStringList fileName = QFileDialog::getOpenFileNames(this,tr("Open Image"),
"C:/qt-win-opensource-src-4.5.0/bin/",
tr("Image Files(*.png *.jpg *.bmp *.avi *.gif)"));
iterator = new QStringListIterator(fileName);
label = new QLabel;
if(iterator->hasNext())
{
label->clear();
label->setPixmap(QPixmap(iterator->next()));
label->show();
}
}
You should use a scroll area for all those images you want to be displayed. You can set a layout depending on how you want those images to be arranged and display them using instances of QLabel.
iterator = new QStringListIterator(fileName);
label = new QLabel;
if(iterator->hasNext())
{
label->clear();
label->setPixmap(QPixmap(iterator->next()));
ui->scrollArea->layout()->addWidget(label); // need to add a scroll area widget in your ui file
// and set layout to it (horizontal, vertical, grid etc.)
}
This way, should be no problem and your labels should be displayed properly.
I want to set QTableWidgetItem's data as an image. imagePath may be different each time.
QTableWidgetItem *itemMedia = new QTableWidgetItem();
itemMedia->setTextAlignment(Qt::AlignCenter);
itemMedia->setData(Qt::DecorationRole, QPixmap(imagePath).scaled(width, 100, Qt::KeepAspectRatio, Qt::SmoothTransformation));
m_table->setItem(0,0,itemMedia);
m_table->setItem(0,1,itemMedia);
m_table->setItem(1,0,itemMedia);
m_table->setItem(1,1,itemMedia);
I've created it nicely. Next, I want to get data with this:
connect(m_table, SIGNAL(itemClicked(QTableWidgetItem *)), this, SLOT(onItemClicked(QTableWidgetItem *)));
void MUCSharedMedia::onItemClicked(QTableWidgetItem *item)
{
qDebug()<<"DecorationRole: " <<item->data(Qt::DecorationRole).toString();
qDebug()<<"DisplayRole: " <<item->data(Qt::DisplayRole).toString();
}
Actually I want imagePath in one of this role , but I get this line in Application Console:
DecorationRole: ""
DisplayRole: ""
How to get value? Any suggestion?
EDITED:
I want to show image on each QTableWidgetItem after that I want to store image path of images which I've shown.
If you need to store QString actually, you need DisplayRole two times:
itemMedia->setData(Qt::DisplayRole, imagePath);
qDebug()<<"DisplayRole: " <<item->data(Qt::DisplayRole).toString();
EDIT: if you need to show image and get image file path I suggest you another way:
1) Set image like you did:
itemMedia->setData(Qt::DecorationRole, QPixmap(imagePath).scaled(width, 100, Qt::KeepAspectRatio, Qt::SmoothTransformation));
2) Set image path using Qt::UserRole
itemMedia->setData(Qt::UserRole, imagePath);
When you need it:
qDebug()<<"File Path: " <<item->data(Qt::UserRole).toString();
But application will use image for displaying.
QTableWidgetItem::data() returns QVariant where you'll get the data with QVariant::value().
Alternatively, use QTableWidget::text().
http://doc.qt.io/qt-5/qtablewidgetitem.html
You store a QPixmap:
itemMedia->setData(Qt::DecorationRole, QPixmap(imagePath).scaled(width, 100, Qt::KeepAspectRatio, Qt::SmoothTransformation));
but try to extract it as a QString:
qDebug()<<"DecorationRole: " <<item->data(Qt::DecorationRole).toString();
That will always give you a default-constructed (i.e. empty) QString.
You want to retrieve it as a QPixmap:
item->data(Qt::DecorationRole).value<QPixmap>()
(though there's little point sending that to a QDebug stream!)
There's a good chance you want the original, unscaled pixmap. In which case, you'll need to store that as well, perhaps in Qt::UserRole:
itemMedia->setData(Qt::UserRole, QPixmap(imagePath));
and change the retrieval to match.
I am trying to output a QWidget ui to a pdf file like this;
const QString filename = "class1bills.pdf";
printer = new QPrinter(QPrinter::HighResolution);
printer->setOutputFormat(QPrinter::PdfFormat);
printer->setOrientation(QPrinter::Portrait);
printer->setPaperSize(QPrinter::A4);
printer->setPageSize(QPrinter::A4);
printer->setPageMargins (15,15,15,15,QPrinter::Millimeter);
printer->setFullPage(false);
printer->setOutputFileName(filename);
painter = new QPainter(printer);
class1Bill->render(painter, QPoint(), QRegion(), QWidget::DrawChildren | QWidget::DrawWindowBackground);
painter->begin(printer);
painter->end();
assert(QFile::exists(filename));
class1Bill is an object of the class that inherits QWidget. Everything works fine but when i open the pdf file, i expect the widget ui to appear fitting the A4 page size but it appears very small that i didn't even see it at first glance. How do i make the widget ui fit the A4 paper size i set?
Set printer->setFullPage(true); in order to fit paper size
How do I set a drop-shadow effect on a QPushButton text?
I could set shadow on the entire QPushButton using QGraphicsDropShadowEffect, I however, am not able to find a way to directly set the effect of text inside the QButton.
Edit:
I tried the following, not sure if the syntax is correct
in the .qss file:
MyButton::text
{
shadow: palette(dark);
}
I set the button's drop shadow effect by:
QGraphicsDropShadowEffect* effect = new QGraphicsDropShadowEffect( );
effect->setBlurRadius( 5 );
mStartButton->setGraphicsEffect( effect );
Try this:
Set a QLabel iside QPushButton rather than simple text. Then apply shadow effect to the label.
You may need to add extra code for centering the label inside the pushbutton.
mStartButton->setText("");
QLabel *label = new QLabel(mStartButton);
label->setText("<b>Button</b>");
QGraphicsDropShadowEffect* effect = new QGraphicsDropShadowEffect( );
effect->setBlurRadius( 5 );
label ->setGraphicsEffect( effect );
the following code plays the song while a button is pressed (onclick). I have created a horizontalSlider UI but it is not sliding. I got the document from http://wiki.forum.nokia.com/index.php/Streaming_Audio_with_Qt
Please help me to make the slider work.
Phonon::MediaObject *mediaObject = new Phonon::MediaObject(this);
Phonon::AudioOutput *audioOutput =
new Phonon::AudioOutput(Phonon::MusicCategory, this);
Phonon::createPath(mediaObject, audioOutput);
const QString url("c://example.mp3");
mediaObject->setCurrentSource(url);
mediaObject->play();
volumeSlider = new Phonon::VolumeSlider(ui->horizontalSlider);
volumeSlider->setAudioOutput(audioOutput);
volumeSlider->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
Probably you didn't bind the corresponding media object to the slider.
See a small example here.