I'm trying to open the file explorer dialog from a dialog window using Qt and C++. My "dialog.cpp" file looks like this:
#include "dialog.h"
#include "ui_dialog.h"
#include "QFileDialog"
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::on_pushButton_Browse_clicked()
{
QString filename = QFileDialog::getOpenFileName(this, tr("Open File"), "C://", "All files (*.*)");
}
Everywhere I have looked says that this should work without a problem. So I tried putting the same exact code in my "mainwindow.cpp" file. Which is this:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "ui_dialog.h"
#include "QFileDialog"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_Open_dialog_clicked()
{
dialogUi.setupUi(dialog);
dialog->show();
}
void MainWindow::on_pushButton_Save_clicked()
{
QString filename = QFileDialog::getOpenFileName(this, tr("Open File"), "C://", "All Files (*.*)");
}
This opens up the file explorer dialog with no issues. However, I want to open the file explorer from the dialog window that is opened by the main window, which isn't working even though it's the same exact code. I even added a new MainWindow class ("mainwindow2.cpp") and tried from that to see if maybe it was the Dialog class causing an issue, but the file explorer wouldn't open from that either. It only seems to work from the primary MainWindow class. (By this I mean any window opened from "mainwindow.cpp" cannot open a file explorer dialog, but "mainwindow.cpp" can open the explorer)
Related
I want to design a program including a pushButton and a lineEdit object in Qt with C++ that when the pushButton is clicked, it creates a label on the program and then sets its text to the written text in the lineEdit.
As you see inside void MainWindow::on_pushButton_clicked() function in the code below, I defined in my code to create an object of QLabel type and show the written text in the lineEdit but when I build and run my program, I don't see anything and it looks like the function didn't create the label. What's wrong with my code and what should I do?
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QLabel>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
QLabel *label = new QLabel("Name: ", this);
label->setGeometry(10, 10, 150, 15);
label->setText(ui->lineEdit->text());
}
By the way, I'm new to Qt and that's why I accept other issues in my code. Thanks for helping.
I am creating media player with qt5. Code below works fine ( it open file dialog, load one song and play it after loading) but it can't play some MP3 files. These files are not damaged or something like this, I can open them with other media players. I noticed that the most of them are larger than 20 mB. Please help me if you find the solution.
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFile>
#include <QFileDialog>
#include <QDebug>
#include <iostream>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(&player, &QMediaPlayer::mediaStatusChanged,
this, [&](QMediaPlayer::MediaStatus status){
if(status == QMediaPlayer::LoadedMedia) //play if loaded
player.play();
});
connect(&player , QOverload<QMediaPlayer::Error>::of(&QMediaPlayer::error), // notify about errors
[=](QMediaPlayer::Error error){
if (error==QMediaPlayer::ResourceError)
qDebug("resource");
if (error==QMediaPlayer::FormatError)
qDebug("format");
if (error==QMediaPlayer::AccessDeniedError)
qDebug("acces");
if (error==QMediaPlayer::ServiceMissingError)
qDebug("service");
});
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{ QString file= QFileDialog::getOpenFileName(); //select file
player.setMedia(QUrl::fromLocalFile(file)); // load
}
I'm trying to add a save as feature to a simple text editor I'm making with C++ and QT. I'm attempting to close the current tab when you save your file and open a new tab with the same index and has the name of the new file as the tab title. This is my code:
QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"), "", tr("All Files (*)"));
if (fileName.isEmpty())
return;
else
{
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly))
{
QMessageBox::information(this, tr("Unable to open file"),
file.errorString());
return;
}
QTextStream out (&file);`
out << ui->plainTextEdit->toPlainText();
QFileInfo FileData(file);
int currentTab = ui->tabWidget->currentIndex();
ui->tabWidget->removeTab(currentTab);
QTextStream InputData(&file);
ui->tabWidget->insertTab(currentTab, new Form(), FileData.fileName());
ui->tabWidget->setCurrentIndex(currentTab);
ui->plainTextEdit->setPlainText(InputData.readAll());
file.flush();
file.close();
}
When I try to save the new file, it saves the file to the selected location and replaces the current tab with the file name, but it doesn't write the file to the text window. Any help would be great.
Here is my demo code and it seems to work:
form.cpp which has a plainTextEdit
#include "form.h"
#include "ui_form.h"
Form::Form(QWidget *parent) :
QWidget(parent),
ui(new Ui::Form)
{
ui->setupUi(this);
}
Form::~Form()
{
delete ui;
}
QString Form::GetText()
{
return ui->plainTextEdit->toPlainText();
}
void Form::SetText(QString text)
{
ui->plainTextEdit->setPlainText(text);
}
And the mainwindow.cpp which contains a QTabWidget
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "form.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
ui->tabWidget->insertTab(0, new Form(), "My File");
ui->tabWidget->setCurrentIndex(0);
}
void MainWindow::on_pushButton_2_clicked()
{
int currentIndex = ui->tabWidget->currentIndex();
//Save the text to a file or a variable...
QString content = static_cast<Form*>(ui->tabWidget->widget(currentIndex))->GetText();
ui->tabWidget->removeTab(currentIndex);
ui->tabWidget->insertTab(currentIndex, new Form(), "Saved File");//new name
static_cast<Form*>(ui->tabWidget->widget(currentIndex))->SetText(content);
ui->tabWidget->setCurrentIndex(currentIndex);
}
It is working for me, please try.
Thank You.
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.
This is the main window so far and the second window is a dialog window. How do I get the text from a textbox on window2 when it closes?
Thanks.
#include "mainwindow.h"
#include "window2.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->actionExit, SIGNAL(triggered()), this, SLOT(closeProgram()));
connect(ui->openWindowBtn, SIGNAL(clicked()), this, SLOT(openSecondWindow()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::openSecondWindow()
{
Window2 w2;
w2.exec();
}
void MainWindow::closeProgram()
{
close();
}
Found Solution
All I had to do is create a getString() function in the Window2 class to retreive the text from ui->...
QString Window2::getString()
{
return ui->textEdit->text();
}
Look at your .ui file in designer (or the resulting generated file from the uic), and access the QLineEdit object by name (the same way you connect that signal). You can retrieve the text with the lineEdit::text() accessor.