Qt QLabel default text - c++

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...

Related

Qt ignore click when dismissing popup on Windows

I have an issue with Qt where the behavior on Windows is different than Mac or Linux. I discovered the issue on PySide2 but was able to reproduce it in a minimal C++ application as well (see below).
When I dismiss a Popup widget by clicking outside the click is ignored by the rest of the application. This is the desired behavior and is the way it works on Linux and Mac. However in Windows this click is registered by the widget that was clicked which in my application leads to unwanted user input on the underlying widgets.
Is there a way to will prevent the dismissal click from being passed on in Windows? I am fine with having platform dependent code for this issue.
The behavior can be reproduced with this example. When the popup is open and testButton is clicked the onTestButton method will be executed.
#include "mainwindow.h"
#include <QPushButton>
#include <QHBoxLayout>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
this->setFixedHeight(600);
this->setFixedWidth(800);
QWidget* w = new QWidget();
QDialog* popUp = new QDialog();
popUp->setFixedHeight(200);
popUp->setFixedWidth(200);
popUp->setWindowFlag(Qt::Popup | Qt::FramelessWindowHint);
popUp->setModal(true);
popUp->setVisible(false);
QHBoxLayout *layout = new QHBoxLayout(this);
w->setLayout(layout);
QPushButton* openButton = new QPushButton("open popup");
// same behavior whether using QDialog::show or QDialog::exec
connect(openButton, &QPushButton::clicked, popUp, &QDialog::show);
QPushButton* testButton = new QPushButton("catch mouse");
connect(testButton, &QPushButton::clicked, this, &MainWindow::onTestButton);
layout->addWidget(openButton);
layout->addWidget(testButton);
this->layout()->addWidget(w);
}
void MainWindow::onTestButton()
{
qDebug() << "caught mouse";
}
QWidget is just a widget, if you want it to "hold" focus, then use QDialog instead.
Also it changes the "logic" if u use it as QDialog.show or QDialog.exec.
Can you try with QDialog?
QDialog::setModal(bool) should set the dialog to modal, thus prohibiting clicking into the mainwindow.
Your windowflag Qt::Popup blocked modality. If you want to use that approach and create Qwidget as a modal dialog (Qt::Dialog), it should be launched from another window, or have a parent and used with the QWidget::windowModality property.
Tested:
auto w = new QWidget;
QDialog* popUp = new QDialog;
popUp->setFixedHeight(200);
popUp->setFixedWidth(200);
popUp->setWindowFlags( Qt::FramelessWindowHint);
popUp->setModal(true);
QHBoxLayout *layout = new QHBoxLayout(this);
w->setLayout(layout);

QtListWidgetItem with Pixmap crashes if to many

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

Dynamically add QWebEngineView to layout

I'm trying to dynamically QWebEngineView to already existing layout.
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QWebEngineView view;
view.setUrl(QUrl(QStringLiteral("http://www.qt.io")));
view.resize(1024, 750);
view.show();
ui->splitter->addWidget(view);
}
When running this i'm getting error: C2664: 'void QSplitter::addWidget(QWidget *)': cannot convert argument 1 from 'QWebEngineView' to 'QWidget *'
I'm trying to create program for previewing and editing html/text/image files in local file system. This means that i need to switch widget in main window for different tasks. In my designer form I have splitter layout in which I'm trying to add QWebEngineView.
I tried default examples from Qt Designer for WebEngine and WebKit. They work just as planned but instead of using UI layout they are using only code for adding and managing widgets. I want to use form layouts, which means that this option is not suited for me.
How i can fix this issue ?
Is this viable solution for what i'm trying to achieve or there is a better one ?
Try this way:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QWebEngineView *view;
view = new QWebEngineView(this);
view->setUrl(QUrl(QStringLiteral("http://www.qt.io")));
view->resize(1024, 750);
view->show();
ui->splitter->addWidget(view);
}

Qt C++ Creating toolbar

I am learning Qt and trying some examples in the book "Foundations of Qt Development".
In the book, there is a section teaching Single Document Interface with an example creating a simple app like a notepad.
However I am having problem with toolbar creating.
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setAttribute(Qt::WA_DeleteOnClose);
setWindowTitle(QString("%1[*] - %2").arg("unnamed").arg("SDI"));
connect(ui->docWidget->document(), SIGNAL(modificationChanged(bool)), this, SLOT(setWindowModified(bool)));
createActions();
createMenu();
createToolbars();
statusBar()->showMessage("Done");
}
It is the constructor of the main window.
void MainWindow::createToolbars()
{
QToolBar* toolbar;
toolbar = addToolBar(tr("File"));
toolbar->addAction(anyaction);
}
This is how the book create the toolbar.
However, when I try to run the program, there are two toolbars created.
One is the toolbar created by the code and called "File"
Another is a blank toolbar created by the ui designer ie. *ui.toolbar.
In order to get rid of two toolbars, I tried using only the *ui.toolbar.
It's working. The code is shown below.
void MainWindow::createToolbars()
{
ui->toolBar->addAction(anyaction);
}
But I tried to create the toolbar by code only, ie. not adding a toolbar in the ui designer.
So I write this:
void MainWindow::createToolbars()
{
QToolBar* FileBar = this->addToolBar(tr("File"));
FileBar->addAction(anyaction);
}
However, there is a compile error.
The compiler use this function:
void QMainWindow::addToolBar(QT::ToolBarArea area, QToolBar * toolbar)
instead of what I want:
QToolBar * QMainWindow::addToolBar(const QString & title)
http://doc.qt.io/qt-5/qmainwindow.html#addToolBar-3
What is my mistake here?
When you removed QToolBar from MainWindow QtCreator automatically removed import of QToolBar class.
Just add this to the top of mainwindow.h:
#include <QToolBar>
And it is better to define QToolBar* FileBar in private section of MainWindow in mainwindow.h. Then you will be able to access it from any method of MainWindow class.
void MainWindow::createToolbars()
{
FileBar = this->addToolBar(tr("File"));
FileBar->addAction(anyaction);
}
When you see such message:
must point to class/struct/union/generic type
First of all try to include headers for necessary class.

Create QAction with shortcut, without inserting in menu

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <cassert>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QAction* back = new QAction(this);
back->setVisible(true);
back->setShortcut(QKeySequence("Ctrl+M"));
bool cres = connect(back, SIGNAL(triggered(bool)), this, SLOT(mySlot()));
assert(cres);
}
In this code I tried to catch Ctrl+M key event. I don't want to put the action in menu. connect returns true but mySlot is never called. When action is inserted in menu, shortcut works well. What I have done wrong?
QAction is dormant until you insert it somewhere. As vahancho has suggested, use QShortcut. You need to instantiate the shortcut for each top-level widget (window) where you want it to be active. Thus if you have 5 top-level windows, you'll need 5 shortcuts, each having one of windows as its parent.
There is no way to use QShortcut as a global shortcut without the gui. QShortcut is only active when its associated widget has focus. The widget could be a top-level window.
System-global shortcuts are the subject of this question.