I have a simple example with QGraphicsScene wrong behavior
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsItem>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
QGraphicsScene * scene = new QGraphicsScene(this);
ui->graphicsView->setScene(scene);
//background
ui->graphicsView->setBackgroundBrush(Qt::black);
QLineF y_line(ui->graphicsView->sceneRect().center().x(), ui->graphicsView->sceneRect().center().y() - 100,
ui->graphicsView->sceneRect().center().x(), ui->graphicsView->sceneRect().center().y() - 550);
QLineF x_line(ui->graphicsView->sceneRect().center().x() - 100, ui->graphicsView->sceneRect().center().y(),
ui->graphicsView->sceneRect().center().x() - 550, ui->graphicsView->sceneRect().center().y());
scene->addLine(y_line, QPen(Qt::yellow));
//add line X
scene->addLine(x_line, QPen(Qt::green));
ui->graphicsView->fitInView(ui->graphicsView->sceneRect());
}
MainWindow::~MainWindow()
{
delete ui;
}
I uave this:
And this is ok, i think
BUT
if i comment out add line X
i have this :
How you can see the line direction!! changed .
How somebody can explain this???
Wrong behavior i think? some bugs in Qt?
Related
I tried to set axis labels on Q3DSurface by doing the following:
#include "mainwindow.h"
#include <Q3DSurface>
using namespace QtDataVisualization;
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
Q3DSurface *graph = new Q3DSurface;
QWidget *widget = QWidget::createWindowContainer(graph);
setCentralWidget(widget);
graph->axisX()->setLabels(QStringList{"a", "b", "c"});
}
MainWindow::~MainWindow() {}
But the labels are not showing, any hint on how to show them?
Setting this property for QValue3DAxis does nothing, as it generates labels automatically.
https://doc.qt.io/qt-5/qabstract3daxis.html#labels-prop
I think you wants to set axis titles, not labels. The titles hidden by default
#include "mainwindow.h"
#include <Q3DSurface>
using namespace QtDataVisualization;
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
Q3DSurface *graph = new Q3DSurface;
QWidget *widget = QWidget::createWindowContainer(graph);
setCentralWidget(widget);
m_graph->axisX()->setTitle(QStringLiteral("a"));
m_graph->axisY()->setTitle(QStringLiteral("b"));
m_graph->axisZ()->setTitle(QStringLiteral("c"));
m_graph->axisX()->setTitleVisible(true);
m_graph->axisY()->setTitleVisible(true);
m_graph->axisZ()->setTitleVisible(true);
}
MainWindow::~MainWindow() {}
i have a window with a Pushbutton that hides the window to system tray when it is pushed.
The problem is that no system tray icon is shown.
Here is the code, what i do wrong?
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QSystemTrayIcon>
#include <QString>
#include <QPixmap>
#include <QIcon>
#include <QAction>
#include <QMenu>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//setup tray icon
QSystemTrayIcon *tray;
QPixmap icon(":/pencil.png");
QIcon trayIcon;
tray= new QSystemTrayIcon(this);
trayIcon= QIcon(icon);
tray->setIcon(trayIcon);
tray->setToolTip("ToolTip");
//setup restore
QAction *restoreAction;
restoreAction = new QAction(QIcon(":/pencil.png"), "Restore", this);
connect(restoreAction, SIGNAL(triggered()), this, SLOT(show()));
QMenu *trayIconMenu;
trayIconMenu = new QMenu(this);
trayIconMenu->addAction(restoreAction);
tray->setContextMenu(trayIconMenu);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
this->hide();
}
I press the button the window is being hidden but no systray icon show up.
Why?
well i found it....
i had to set visible to true tray icon
trayIconMenu = new QMenu(this);
trayIconMenu->addAction(restoreAction);
tray->setContextMenu(trayIconMenu);
tray->setVisible(true);
Hi I have a problem with Phonon and a slot, it's me first try with this and I hope you could help me,
#include <QMainWindow>
#include <QWidget>
#include <phonon/VideoPlayer>
#include <phonon/VideoWidget>
#include <phonon/MediaObject>
#include <phonon/MediaSource>
#include <phonon>
#include <QVBoxLayout>
#include <QFileDialog>
#include <QPushButton>
#include <QUrl>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
QPushButton *quit;
QPushButton *addFile;
QWidget *Main;
Phonon::VideoPlayer *player;
public slots:
void startVideo();
};
my header ^
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
Main = new QWidget;
addFile = new QPushButton("Dodaj Plik");
quit = new QPushButton("Zamknij");
player = new Phonon::VideoPlayer(Phonon::VideoCategory, Main);
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(addFile);
layout->addWidget(quit);
layout->addWidget(player);
Main->setWindowTitle("Phonon");
Main->setLayout(layout);
Main->setGeometry(550, 312, 640, 480);
Main->show();
QObject::connect(addFile, SIGNAL(clicked()), player, SLOT(startVideo()));
QObject::connect(quit, SIGNAL(clicked()), Main, SLOT(close()));
}
MainWindow::~MainWindow()
{
}
void MainWindow::startVideo()
{
QString file = QFileDialog::getOpenFileName(this, tr("odtworz film"));
player->play(file);
}
source ^
For now application runs without problems but when I click "dodaj plik" nothing happens and debugger says it have no such a slot like startVideo()
Can you help me to figure out?
The problem is that you establish connection like this:
QObject::connect(addFile, SIGNAL(clicked()), player, SLOT(startVideo()));
however your startVideo() slot defined in the MainWindow class. Thus, the correct connection should look like:
connect(addFile, SIGNAL(clicked()), this, SLOT(startVideo()));
The QObject:: prefix is not needed, as the QMainWindow - the base class, already is QObject.
i am doing Qt project about displaying many texts. in detail, after 1st text display, it will close then display next file. My problem here was that just the last file displayed. all link resource paths are correct. Please help me fix me. Thanks in advance
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFile>
#include <QTextStream>
#include <QMessageBox>
#include <QString>
#include <QStackedWidget>
#include <QTextBrowser>
#include <QStringList>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QStringList L;
L << ":/sample.txt" << ":/idp.txt";
foreach (QString str, L){
QFile file(str);
if (!file.open(QIODevice::ReadOnly))
QMessageBox::information(0,"error file path", file.errorString());
QString name = file.fileName();
QStringList parts = name.split("/");
QString lastBit = parts.at(parts.size()-1);
statusBar()->showMessage(lastBit);
QTextStream out(&file);
QString txt = out.readAll();
QStackedWidget *temp = new QStackedWidget();
QTextBrowser *textbrs = new QTextBrowser();
textbrs->setText(txt);
temp->addWidget(textbrs);
setCentralWidget(temp);
file.close();
}
}
MainWindow::~MainWindow()
{
delete ui;
}
Something like this:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFile>
#include <QTextStream>
#include <QMessageBox>
#include <QString>
#include <QStackedWidget>
#include <QTextBrowser>
#include <QStringList>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
L << ":/sample.txt" << ":/idp.txt"; //Class member of Type QStringList
textbrs = new QTextBrowser(); //Class member of Type QTextBrowser*
ui->centralWidget->layout()->addWidget(textbrs);
timer = new QTimer(); //Class member of Type QTimer*
timer->setInterval(5000);
connect(timer,SIGNAL(timeout()),this,SLOT(slotFileAction()));
timer->start();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::slotFileAction()
{
static int count = 0;
if(count >= L.size())
timer->stop();
QString str = L.at(count);
count++;
QFile file(str);
if (!file.open(QIODevice::ReadOnly))
QMessageBox::information(0,"error file path", file.errorString());
QString name = file.fileName();
QStringList parts = name.split("/");
QString lastBit = parts.at(parts.size()-1);
statusBar()->showMessage(lastBit);
QTextStream out(&file);
QString txt = out.readAll();
textbrs->setText(txt);
file.close();
}
Each time you will replace the old QStackedWidget by
setCentralWidget(temp);
The example to use QStackedWidget in Qt help document is following:
QWidget *firstPageWidget = new QWidget;
QWidget *secondPageWidget = new QWidget;
QWidget *thirdPageWidget = new QWidget;
QStackedWidget *stackedWidget = new QStackedWidget;
stackedWidget->addWidget(firstPageWidget);
stackedWidget->addWidget(secondPageWidget);
stackedWidget->addWidget(thirdPageWidget);
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(stackedWidget);
setLayout(layout);
So, you should add multiple QTextBrowsers in QStackedWidget by addWidget() and use setCentralWiget() just once.
Hope helpful.
#include <QtCore>
#include <QtGui>
#include <QLabel>
#include <QScrollArea>
#include <QScrollBar>
#include <QFileDialog>
#include"QDebug"
#include<math.h>
#include<QApplication>
QScrollArea* scrollarea = new QScrollArea;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->label->setBackgroundRole(QPalette::Base);
ui->label->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
ui->label->setScaledContents(true);
scrollarea->setBackgroundRole(QPalette::Dark);
scrollarea->setWidget(ui->label);
setCentralWidget(scrollarea);
QImage img("D:\\picture.jpg");
ui->label->setPixmap(QPixmap::fromImage(img));
}
MainWindow::~MainWindow()
{
delete ui;
}
i am newbie in Qtcreator.
i try to add scroll bar to label. but when i run, it occurs an error:
QWidget: Must construct a QApplication before a QPaintDevice
how should i do to fix this
QScrollArea* scrollarea = new QScrollArea;
You are constructing a global QScrollArea before QApplication is created. Make it a member variable of MainWindow.