Controlling Multiple UI files in Qt framework - c++

Question asked twice: see Handling multiple ui files in Qt
I am new to Qt framwork , i have been given this simple task:
In the MainWindow , i have a submit button , once its clicked another total different window should appear
i thought of doing this by doing one extra UI file called From.ui file and to switch from MainWindow to Form once submit is clicked , this is my code:
//main.cpp
#include "mainwindow.h"
#include <QtGui/QApplication>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow mainWindow;
mainWindow.setOrientation(MainWindow::ScreenOrientationAuto);
mainWindow.showExpanded();
return app.exec();
}
//MainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "form.h"
#include <QtCore/QCoreApplication>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow:: SubmitClicked()
{
Form* f= new Form(this);
f->show();
f->raise();
f->activateWindow();
}
//Form.cpp
#include "form.h"
#include "ui_form.h"
Form::Form(QWidget *parent) :
QWidget(parent),
ui(new Ui::Form)
{
ui->setupUi(this);
}
Form::~Form()
{
delete ui;
}
this code compiled perfectly , but its not doing as expected , once submit is clicked , nothing is done...
can you please tell me whats wrong ?

It seems that SubmitClicked slot is not connected to clicked event of your button
Put a cout/printf at the top of your SubmitClicked method to make sure that it is called.

Related

Why do I get the "qt.pointer.dispatch" error?

I am making a little app in Qt Creator, and I keep getting this error/warning from qt.pointer.dispatch:
qt.pointer.dispatch: delivering touch release to same window QWindow(0x0) not QWidgetWindow(0x14ef0d570, name="MainWindowWindow")
qt.pointer.dispatch: skipping QEventPoint(id=1 ts=0 pos=0,0 scn=1063.83,670.067 gbl=1063.83,670.067 Released ellipse=(1x1 ∡ 0) vel=0,0 press=-1063.83,-670.067 last=-1063.83,-670.067 Δ 1063.83,670.067) : no target window
I'm not doing anything special with touch release (as you can see below), and the error also doesn't affect the running of the program so far as I can tell. What is the problem and how do I fix it?
main.cpp
#include "mainwindow.hpp"
#include <QApplication>
#include <QLocale>
#include <QTranslator>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTranslator translator;
const QStringList uiLanguages = QLocale::system().uiLanguages();
for (const QString &locale : uiLanguages) {
const QString baseName = "Simulization_" + QLocale(locale).name();
if (translator.load(":/i18n/" + baseName)) {
a.installTranslator(&translator);
break;
}
}
MainWindow w;
w.show();
return a.exec();
}
mainwindow.cpp
#include "mainwindow.hpp"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}

qInputDialog make visible Unity taskbar and titlebar in fullscreen application

I'm trying to ask for a password the user in order to access a particular section, my application is fullscreen. The problem is that when the qInputDialog appears also the unity taskbar and the application titlebar appears. I want to avoid this and keep my application fullscreen.
I'm using Qt 5.12.3 on Ubuntu 16.04
Take a look at this simple example:
Main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.showFullScreen();
return a.exec();
}
MainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QInputDialog>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
bool ok;
QString text = QInputDialog::getText(this, tr("Restriscted"),
tr("Password:"), QLineEdit::Password,"",&ok, Qt::FramelessWindowHint);
if (ok && text=="pass")
{
ui->label->setText("ok");
}
}
Seems there is no solution for this at the moment and the issue depends on Unity. At the end I replaced the qInputDialog with a qStackedView page with a qLabel and a qButton on it, then I hide/show the page accordingly.

Filtering ListView & QFileSystemModel Qt

I've got a strange problem with filtering QFileSystemModel
In "dialog.ui" there is only a QListView.
main.cpp
#include "dialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
w.show();
return a.exec();
}
dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include <QtCore>
#include <QtGui>
#include <QFileSystemModel>
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
private:
Ui::Dialog *ui;
QFileSystemModel* fileModel;
};
#endif // DIALOG_H
dialog.cpp
#include "dialog.h"
#include "ui_dialog.h"
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
fileModel = new QFileSystemModel(this);
fileModel->setFilter(QDir::Files);
ui->listView->setModel(fileModel);
ui->listView->setRootIndex(fileModel->setRootPath("/"));
////////////////////////////////// - Problem!
fileModel->setRootPath("/home/");
fileModel->setRootPath("/");
//////////////////////////////////
}
Dialog::~Dialog()
{
delete ui;
}
The problem is I see "home" folder (only this one) on the list, although filtering is set to QDir::Files. How to delete this entry?
If you want a file explorer with tree view showing folders and list view showing files, change QListView to QTreeView, Use the following constructor code, which is portable:
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
fileModel = new QFileSystemModel(this);
fileModel->setFilter(QDir::Files);
ui->treeView->setModel(fileModel);
auto myhome = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
////////////////////////////////// - Problem!
ui->treeView->setRootIndex(fileModel->setRootPath(myhome));
ui->treeView->setRootIndex(fileModel->setRootPath("/"));
//////////////////////////////////
}

Qt desktop app will not close old dialogs and will not allow more than 2 windows to be open at a time

I am trying to open up my app with a child window first. I got some code that almost works, but I cannot for the life of me figure out what is wrong with it that won't let it delete old dialog windows, and when I click back into the child dialog I opened with, it glitches out and I just end back up at square one! What should I do to stop this problem?
main.cpp file
#include "mainwindow.h"
#include <QApplication>
#include "yes.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget MainWindow;
MainWindow.show();
yes popup(&MainWindow);
popup.show();
return a.exec();
}
.cpp file
#include "yes.h"
#include "ui_yes.h"
#include "mainwindow.h"
yes::yes(QWidget *parent) :
QDialog(parent),
ui(new Ui::yes)
{
ui->setupUi(this);
ui->label->setVisible(false);
}
yes::~yes()
{
delete ui;
}
void yes::on_pushButton_clicked()
{
auto no = new MainWindow();
no->show();
no->setAttribute(Qt::WA_DeleteOnClose);
no->show();
}

QtWebEngine display black block when I called QWidget::winId()

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPushButton>
#include <QWebEngineView>
#include <qDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
#if 1
auto btn = new QPushButton;
ui->gridLayout->addWidget(btn);
qDebug()<<btn->winId();
#endif
auto web = new QWebEngineView;
ui->gridLayout->addWidget(web);
web->load(QUrl("http://www.google.com"));
}
MainWindow::~MainWindow()
{
delete ui;
}
That's the whole code.
Windows 10 , Qt 5.5 .
When I turn on the switch, winId() would be called, then the QtWebEngine can not work rightly.
What should I do ?
Don't call winId() until the widget is visible. You could set an eventFilter that is activated on QEvent::Show, for further information see http://doc.qt.io/qt-5/qobject.html#installEventFilter