I am running on ubuntu 13.10 64bit with installed Qt Creator also i have installed libphonon-dev
my code looks like this:
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <phonon/phonon> // yes directory /usr/include/phonon exists
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
Phonon::MediaObject *music = Phonon::createPlayer(Phonon::MusicCategory, Phonon::MediaSource("/home/erik/Downloads/Bob_Marley-No_Woman_No_Cry.mp3"));
music->play();
}
MainWindow::~MainWindow()
{
delete ui;
}
I am getting 10 same errors:
/usr/include/phonon/phonon:12: In file included from
/usr/include/phonon/phonon:12:0,
/home/erik/QtProjs/QtPlayer/mainwindow.cpp:3: from
../QtPlayer/mainwindow.cpp:3: /usr/include/phonon/effectwidget.h:28:
error: QtGui/QWidget: No such file or directory #include
<QtGui/QWidget> ^ /usr/include/phonon/effectwidget.h
any other project file is default (mainwindow.h, mainwindow.ui, main.cpp)
Yesterday i have made a simple text editor(not from example), but i cant get through this error.
Thankyou
Take a look at this question. It can be similar to your case if you're building a Qt 4 project with Qt 5.
Related
I have tried this code but the button isn't displaying on the main window.
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QGridLayout>
#include<QLabel>
#include<QPushButton>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QPushButton *l=new QPushButton();
l->setText("abc");
QGridLayout *q=new QGridLayout();
q->addWidget(l);
this->setLayout(q);
this->show();
}
MainWindow::~MainWindow()
{
delete ui;
}
I have tried to change the code even by passing enums for alignment but nothing worked.
When you create new Qt widgets Application, the default form (MainWindow ui) is created with centralWidget to put all other widgets. In your code you created the QGridLayout without a parent, typically such layout should be placed in ui->centralWidget (as far as you are not creating another widget to be set as centralWidget), moreover I assume your mainWindow is shown from main.cpp (need not use show()). your code could thus be:
QPushButton *l=new QPushButton();
l->setText("abc");
QGridLayout *q=new QGridLayout(ui->centralWidget);
q-> addWidget(l);
Try adding the widget to the GridLayout with index using addWidget function
void QGridLayout::addWidget(QWidget *widget, int row, int column, Qt::Alignment alignment = ...)
like:
q-> addWidget(l, 0, 0);
P.S. also consider using better names for your variables!
I want to set item for comboBox of QT in function.
I coded below:
Function:
void addItemInComboBoxBaudRate(){
Ui::MainWindow::cbBaudRate->addItem("2400");
Ui::MainWindow::cbBaudRate->addItem("4800");
Ui::MainWindow::cbBaudRate->addItem("9600");
Ui::MainWindow::cbBaudRate->addItem("19200");
Ui::MainWindow::cbBaudRate->addItem("38400");
Ui::MainWindow::cbBaudRate->addItem("57600");
Ui::MainWindow::cbBaudRate->addItem("115200");
Ui::MainWindow::cbBaudRate->setCurrentText("9600)");
}
In main function:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
addItemInComboBoxBaudRate();
}
Error:
/home/san/workspace/build-dieu_khien_san-Desktop-Debug/ui_mainwindow.h:44: error: invalid use of non-static data member 'Ui_MainWindow::cbBaudRate'
QComboBox *cbBaudRate;
/home/san/workspace/dieu_khien_san/mainwindow.cpp:20: error: from this location
Ui::MainWindow::cbBaudRate->addItem("2400");
change the code into
void addItemInComboBoxBaudRate(){
Ui::MainWindow->cbBaudRate->addItem("2400");
Ui::MainWindow->cbBaudRate->addItem("4800");
Ui::MainWindow->cbBaudRate->addItem("9600");
Ui::MainWindow->cbBaudRate->addItem("19200");
Ui::MainWindow->cbBaudRate->addItem("38400");
Ui::MainWindow->cbBaudRate->addItem("57600");
Ui::MainWindow->cbBaudRate->addItem("115200");
Ui::MainWindow->cbBaudRate->setCurrentText("9600)");
}
edited: try that way (without the mainwindow.h code I'm just guessing), if it doesn't work, please edit your question with full header file (or a least all the MainWindow class)
I am just staring Qt, so hopefully is a rookie question. Working in Qt Creator 2.7.2, Qt 5, standard desktop app.
Currently my app is one window, with the main window entirely taken up by a console object, with is just a plain text edit, like this:
setCentralWidget(console);
Which of course takes up the entire window. So I added a frame using the UI editor, frame_2. How do I get the console to appear inside the frame, instead of taking up the whole window?
http://qt-project.org/doc/qt-4.8/designer-layouts.html
http://qt-project.org/doc/qt-4.8/layout.html
Your central widget is just QWidget, and then it needs a layout. You drag a layout into it, and there are some buttons across the top of Qt Designer for turning on and off the layout.
When setupUi is called, it already does the setCentralWidget call for you. If you call it yourself you lose all you gained from using Qt Designer.
ui->setupUi(this);
If you are using Qt Designer, you should not edit any generated files. If you edit the .ui file again, it may generate the ui_.h file for you again and fix the problem.
Here is an example of the generated file ui_mainwindow.h:
/********************************************************************************
** Form generated from reading UI file 'mainwindow.ui'
**
** Created: Wed Jul 10 15:48:32 2013
** by: Qt User Interface Compiler version 4.8.4
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/
#ifndef UI_MAINWINDOW_H
#define UI_MAINWINDOW_H
#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QHeaderView>
#include <QtGui/QMainWindow>
#include <QtGui/QMenuBar>
#include <QtGui/QStatusBar>
#include <QtGui/QToolBar>
#include <QtGui/QWidget>
QT_BEGIN_NAMESPACE
class Ui_MainWindow
{
public:
QMenuBar *menuBar;
QToolBar *mainToolBar;
QWidget *centralWidget;
QStatusBar *statusBar;
void setupUi(QMainWindow *MainWindow)
{
if (MainWindow->objectName().isEmpty())
MainWindow->setObjectName(QString::fromUtf8("MainWindow"));
MainWindow->resize(400, 300);
menuBar = new QMenuBar(MainWindow);
menuBar->setObjectName(QString::fromUtf8("menuBar"));
MainWindow->setMenuBar(menuBar);
mainToolBar = new QToolBar(MainWindow);
mainToolBar->setObjectName(QString::fromUtf8("mainToolBar"));
MainWindow->addToolBar(mainToolBar);
centralWidget = new QWidget(MainWindow);
centralWidget->setObjectName(QString::fromUtf8("centralWidget"));
// !!!!! NOTE !!!!! setCentralWidget gets called
MainWindow->setCentralWidget(centralWidget);
statusBar = new QStatusBar(MainWindow);
statusBar->setObjectName(QString::fromUtf8("statusBar"));
MainWindow->setStatusBar(statusBar);
retranslateUi(MainWindow);
QMetaObject::connectSlotsByName(MainWindow);
} // setupUi
void retranslateUi(QMainWindow *MainWindow)
{
MainWindow->setWindowTitle(QApplication::translate("MainWindow", "MainWindow", 0, QApplication::UnicodeUTF8));
} // retranslateUi
};
namespace Ui {
class MainWindow: public Ui_MainWindow {};
} // namespace Ui
QT_END_NAMESPACE
#endif // UI_MAINWINDOW_H
I also rambled about using Layouts on a previous post:
Add QRadioButton into QWidget without layout
Hope that helps.
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <stdio.h>
#include <iostream>
#include <QDialog>
#include <opencv2\video\video.hpp>
#include <opencv2\opencv.hpp>
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/flann/miniflann.hpp"
#include <QLabel>
#include <QScrollArea>
#include <QScrollBar>
cv::Mat image1;
MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow)
{
image1 = cv::imread("D:\\picture.jpg");
QImage qimage1((uchar*)image1.data,image1.cols,image1.rows,image1.step,QImage::Format_RGB888);
ui->label->setPixmap(QPixmap::fromImage(qimage1));
}
MainWindow::~MainWindow()
{
delete ui;
}
I have picture with size 720*1280. I want to show this picture in label with size 600*600. However,it only shows a part of the picture. So my question is how to show entire picture without changing the size of picture.
You can use function QPixmap::scaled(), see docs here and examples here
In your case it would be something like this:
ui->label->setPixmap(QPixmap::fromImage(qimage1).scaled(QSize(600,600), Qt::KeepAspectRatio));
It won't affect the image itself, it will construct QPixmap from the image, scale it to fit your 600x600 Qlabel and will keep aspect ration. Hope this will help you. By the way, you don't need yo use OpenCV to just read an image, in Qt QImage class can construct QImage with just QString path_to_image: QImage myImg("D:\\picture.jpg");
EDITED (sorry for the delay):
To add QScrollArea, you have to create it in constructor (let's assume, that in your Mainwindow you have only QLabel and QScrollArea) like this:
// constructor, right after ui->setupUi(this);
QScrollArea *scroll=new QScrollArea(this); // creating instance of QScrollarea with mainwindow as it's parent
scroll->setWidget(ui->label); // sets widget, that you want to have scrollbars
this->setCentralWidget(scroll); // sets scrollarea as centralwidget
I am trying to compile a test application for an ARM embedded device. If I use my Desktop Qt SDK version of Qt, this header file compiles just fine. If I use my Linaro GCC for the Embedded target from within Qt Creator, I get the error "'QLCDNumber' does not name a type" on the line of the private declaration for lcdNumber. I've googled around but do not see any solutions that work for this.
Is the problem that my embedded version of qt is missing support for QLCDNumber?
If so, why doesn't it complain about the #include ?
Also, if this is the case, does anyone happen to know what I need to enable in ./configure to enable support for QLCDNumber?
#ifndef BATTERYMONITOR_H
#define BATTERYMONITOR_H
#include <QtGui>
#include <QWidget>
#include <QLabel>
#include <QRadioButton>
#include <QLCDNumber>
class BatteryMonitor : public QWidget
{
Q_OBJECT
public:
BatteryMonitor(QWidget *parent = 0);
public slots:
void clickedBatteryStatus();
private:
QRadioButton *temperature;
QRadioButton *voltage;
QRadioButton *current;
QRadioButton *relativeStateOfCharge;
QRadioButton *batteryStatus;
QRadioButton *runTimeToEmpty;
QLCDNumber *lcdNumber;
};
#endif // BATTERYMONITOR_H
Did it config with QT_NO_LCDNUMBER defined in your embedded Qt SDK? You can check it in file $QTDIR/include/QtCore/qconfig.h.
QLCDNumber and qlcdnumber.h are in the right include path of Qt SDK no matter macro QT_NO_LCDNUMBER defined or not. So there is nothing wrong about #include <QLCDNumber>.