I tried using QSound in my project but I am getting an undefined refrence to _imp_ZN6Q:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtMultimedia/QSound>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
ui->setupUi(this);
QSound player;
QSound::play("E:\songs");
}
MainWindow::~MainWindow()
{
delete ui;
}
In your project file (.pro) add
QT += multimedia
QT += multimedia
and add :
#include <QtMultimedia/QSound>
QSound newMessage("sound/newChat.wave");
newMessage.play();
Related
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
}
#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
I want to do dbus-send from shell/console to a qt application.
This is the code for a simple QT app
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtCore>
#include <QtDBus>
#include <QDBusConnection>
#include <QDebug>
MainWindow::~MainWindow()
{
delete ui;
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
bool ret = QDBusConnection::sessionBus().connect(
"MyHome", //service
"/", //path
"com.mydomain.mcp", //interface
"usb", //name
this, //receiver
SLOT(messageSlot(QString)));
}
void MainWindow::messageSlot(const QString &t1)
{
qDebug("%s", QString("%1").arg(t1).toUtf8().data());
}
From the terminal, I and sending this command
dbus-send --session --print-reply --reply-timeout=2000 --type=method_call / com.mydomain.mcp.usb string:'a'
I get this error: Method "usb" with signature "s" on interface "com.mydomain.mcp" doesn't exist
What am I doing wrong?
Thanks
I have created a Qt GUI application but I haven't touched anything regarding the GUI. I have modified mainwindow.cpp and the project file.
mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QWebPage>
#include <QWebFrame>
QWebPage page;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(page.mainFrame(), SIGNAL(loadFinished(bool)), this, SLOT(pageLoaded1(bool)));
QUrl router("http://192.168.1.1");
page.mainFrame()->load(router);
}
MainWindow::~MainWindow()
{
delete ui;
}
untitled.pro:
#-------------------------------------------------
#
# Project created by QtCreator 2013-05-01T23:48:00
#
#-------------------------------------------------
QT += core gui webkit webkitwidgets
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = untitled
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
main.cpp:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
Error:
---------------------------
Microsoft Visual C++ Debug Library
---------------------------
Debug Error!
Program: ...tled-Desktop_Qt_5_0_2_MSVC2010_32bit-Debug\debug\untitled.exe
Module: 5.0.2
File: global\qglobal.cpp
Line: 1977
ASSERT: "!"No style available without QApplication!"" in file kernel\qapplication.cpp, line 962
(Press Retry to debug the application)
---------------------------
Abort Retry Ignore
---------------------------
Extra characters are inserted here to bypass character requirement.
In main.cpp, make sure you create an application object, even if you don't use directly:
QApplication app;
// Below you can then create the window
Edit
The problem is that you are creating a QWebPage as a global object, and before the QApplication has been created. To solve the problem, make the page a member of the MainWindow class. Also make the page a pointer, otherwise you will get other problems.
i.e. in mainwindow.h:
private:
QWebPage* page;
in mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QWebPage>
#include <QWebFrame>
// Remove this!!
// QWebPage page;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// Create the page here:
page = new QWebPage(this);
connect(page.mainFrame(), SIGNAL(loadFinished(bool)), this, SLOT(pageLoaded1(bool)));
QUrl router("http://192.168.1.1");
page.mainFrame()->load(router);
}
MainWindow::~MainWindow()
{
delete ui;
}
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.