QtListWidgetItem with Pixmap crashes if to many - c++

I'm a noob, so sorry if my question feels dumb.
I use Qt Creator to make a kind of image viewer.
I added a QListWidget and added items with a pixmap. So far, so good.
Now I try to read the hole directory and add all 438 images.
The app crashes with this message:
Cn::Process::NotifyOutOfMemory(). 17:47:36: The program has
unexpectedly finished. 17:47:36: The process was ended forcefully.
If I reduce the count to 85. The app opens, but does only show 77 images.
I tried to fix this by changing addItem to addItems but don't know how to get the QListWidgetItem in a QList or on any other way. And than it is the question of this is a solution.
Can someone give me a kick in the right direction?
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QDir dir("C:/");
QStringList items; // String???
foreach(QFileInfo var, dir.entryInfoList ()){
if(var.isFile ()){
//items += // What to do here ??
ui->listWidget->addItem (new QListWidgetItem(QPixmap(var.absoluteFilePath ()), var.fileName ()));
}
ui->listWidget->addItems (items);
}
}
Michael

Related

On QTreeView double_Clicked event: how to know which folder was clicked?

I'm still pretty new to C++ and qt creator. I have a TreeView displaying a directory, and upon double-clicking a folder I'd like to be able to get the directory of the folder, so i can do something with the contents. I notice the double_Clicked event passing along a "const QModelIndex& index" and I suppose this holds some information on which folder of the tree was clicked. I can't find any documentation on this signal, and what the "index" passed along could be. Does anyone have an explanation, tutorial, example, documentation, ... for me? I've been searching for awhile and trying things out but I can't find the solution. Or additionally: how can I check what gets passed along? How could I print this, or know what it is?
You can set up your treeView in the constructor like this:
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
model = new QFileSystemModel(this);
model->setRootPath("");
ui->treeView->setModel(model);
ui->treeView->setRootIndex(model->index("/home/waqar/"));
}
And in the double_Clicked() slot, use the QModelIndex to get the name of the folder you clicked on:
void MainWindow::on_treeView_doubleClicked(const QModelIndex &index)
{
//displays the name of the folder you clicked on in the terminal
qDebug () << index.data(Qt::DisplayRole).toString();
//get full path
QString path = model->filePath(index)
}

Resizing web page inside a spliter in qt

My question is simple, but I have been struggling to find the solution. I have the QMainWindow showed in the image, constructed in the QtCreator.
I want to load an html web page in the QWidget csWindow, for that I have placed a Qlabel label_pic where I load my web Page. This is code so far:
MainWindow::MainWindow(QWidget *parent, Project *project) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->project = project;
QWebEngineView *view = new QWebEngineView(ui->label_pic);
view->load(QUrl("http://localhost/myWeb.html"));
////works fine, for an image
//QPixmap pix(":/img/imgs/someImage.png");
//ui->label_pic->setPixmap(pix);
//I also can load the web page in the QWidget csWindow but with the same result
//QWebEngineView *view = new QWebEngineView(ui->csWindow);
//view->load(QUrl("http://localhost/myWebb.html"));
}
The page loads fine but it does not fit into the corresponding space, It is created with a fixed size and never resize. I want the web page to be resized when I move the spliters, but I have not succed doing it.
I have tried several approaches, first just put an image in label_pic, enable the property scaled contents and works fine. Now, I want to do the same with the web page.
Thanks in advance.
The page loads fine but it does not fit into the corresponding space
This is because the size of the QWebEngineView is not know until it completes loading, so what you need is to connect to its signal loadFinished and resize label_pic :
connect(view, &QWebEngineView::loadFinished, [this]() {this->ui->label_pic->resize(this->ui->csWindow->size());});
I want the web page to be resized when I move the splitters
Then also you need to connect to signal QSplitter::splitterMoved from all your splitters and resize both csWindow and label_pic like this:
connect(ui->splitter, &QSplitter::splitterMoved, [this]() { this->view->resize(this->ui->csWindow->size()); this->ui->label_pic->resize(this->ui->csWindow->size());});
connect(ui->splitter_2, &QSplitter::splitterMoved, [this]() { this->view->resize(this->ui->csWindow->size()); this->ui->label_pic->resize(this->ui->csWindow->size());});
connect(ui->splitter_3, &QSplitter::splitterMoved, [this]() { this->view->resize(this->ui->csWindow->size()); this->ui->label_pic->resize(this->ui->csWindow->size());});
and note that this would work best if you set a layout for your window, either from designer or adding code, for instance:
QGridLayout *layout = new QGridLayout;
layout->addWidget(ui->splitter_3);
this->ui->centralWidget->setLayout(layout);
and remember you should make all connect statements before you load the view.

QSystemTrayIcon shows only place holder and not the real icon

My code below brings up the icon but its like empty with nothing in it. if I move the mouse cursor over the expected icon location (last one) in system tray, its there but it doesn't show the real icon. It's more like just a place holder for the icon.
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
systemTray = new QSystemTrayIcon();
systemTray->setIcon( QIcon::fromTheme("edit-undo") ); // stock icon but I have tried use local icon file too with same result
systemTray->setVisible( true ); // extra insurance
systemTray->show();
}
What am I doing wrong? I am using Qt 5.4 and Windows 7
I don't know why the stock icon doesn't work but it was a syntax and qmake issue. I had the path given as ":/icons/file.ico" or "icons/file.ico". The correct syntax is below otherwise it will not show the actual icon. Also I had to 'run qmake' apparently needed when new icon is added to qrc because even when the syntax was right, the problem was still there.
systemTray->setIcon( QIcon(":icons/file.ico") );

How to download files from QWebView?

I created a small web browser with QT Creator and QWebView. I's working very good and the pages are loading very fast. But how can I make my browser capable of downloading files? I looked through the signals and functions list, but I did't find something that could help me.
How can I found out if a QUrl contains a link to a file other than text/html so I can download it?
QWebView has a 'QWebPage' member which you can access it's pointer with webView.page() . This is where you should look. QWebPage has two signals: downloadRequested(..) and unsupportedContent(..). I believe dowloadRequest is only emitted when user right clicks a link and selects 'Save Link' and unsupportedContent is emitted when target URL cannot be shown (not an html/text).
But for unsupportedContent to be emitted, you should set forwardUnsupportedContent to True with function webPage.setForwardUnsupportedContent(true). Here is a minimal example I have created:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->webView->page()->setForwardUnsupportedContent(true);
connect(ui->webView->page(),SIGNAL(downloadRequested(QNetworkRequest)),this,SLOT(download(QNetworkRequest)));
connect(ui->webView->page(),SIGNAL(unsupportedContent(QNetworkReply*)),this,SLOT(unsupportedContent(QNetworkReply*)));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::download(const QNetworkRequest &request){
qDebug()<<"Download Requested: "<<request.url();
}
void MainWindow::unsupportedContent(QNetworkReply * reply){
qDebug()<<"Unsupported Content: "<<reply->url();
}
Remember, MainWindow::download(..) and MainWindow::unsupportedContent(..) are SLOTs !

Qt QLabel default text

My code is very simple:
Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget)
{
lineEdit = new QLineEdit();
label = new QLabel("");
connect(lineEdit, SIGNAL(textChanged(QString)), label, SLOT(setText(QString)));
ui->setupUi(this);
}
I compiled it, and no error or warning.
But When I run it, The UI like this:
Why QLabel's default text was TextLabel?
You should read some tutorials from Qt docs. You're mixing QtDesigner ui with manual widget creation. Your default text on label comes from your ui file. Also you don't need to create your labels/line edits when you use ui file. Just get them stright from ui class. So if you'll get your ui file back to normal, then you may do something like this:
Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget)
{
ui->setupUi(this);
connect(ui->lineEdit, SIGNAL(textChanged(QString)), ui->label, SLOT(setText(QString)));
}
Also change text in your label with Qt Designer by doubleclick on it.
That's because both your
lineEdit = new QLineEdit();
label = new QLabel("");
are different that the ones you created in your ui. You are defining two new widgets, while you should probably reference the previous ones:
ui->lineEdit->clear();
ui->label->clear();
connect(ui->line....
//etc...