Why cannot I see QGraphicsLineItem when added to QGraphicsScene in QGraphicsView? - c++

I am trying to add QGraphicsLineItem to QGraphicsScene and display it in QGraphicsView. But when I compile and run the below code, it just shows white blank screen and I am not able to see any line on it. What do you think I am doing wrong?
#include <QApplication>
#include <QGraphicsView>
#include <QGraphicsLineItem>
class MyGraphicsView : public QGraphicsView
{
public:
MyGraphicsView() {
_scene = new QGraphicsScene(0,0,1024,800);
setScene(_scene);
}
virtual void paintEvent(QPaintEvent * e)
{
QGraphicsLineItem *line = new QGraphicsLineItem(100.0,100.0,500.0,500.0);
line->setPen(QPen(Qt::black, 3, Qt::SolidLine));
line->setFlag( QGraphicsItem::ItemIsMovable );
_scene->addItem(line);
}
private:
QGraphicsScene* _scene;
};
int main(int argc, char ** argv)
{
QApplication app(argc, argv);
MyGraphicsView cw;
cw.show();
return app.exec();
}

Related

Coloring a specific line (indicated by the cursor) in textedit for QT , but the cursor's position changes with a function every time button is pressed

I edited it like this. But all text is not printed in textEdit even though the word is added to the correct position and colored.
ui->textEdit->setText(display_text);
QTextcursor cursor=ui->textEdit->textCursor();
cursor.movePosition(QTextCursor::Right,QTextCursor::MoveAnchor,cursor_position);
cursor.insertHtml("<span style=color:red;>"+coloring_string+"</span>");
ui->textEdit->setTextCursor(cursor);
You have to use setExtraSelections:
#include <QApplication>
#include <QTextEdit>
class Editor: public QTextEdit {
public:
Editor(QWidget *parent=nullptr): QTextEdit(parent){
connect(this, &QTextEdit::cursorPositionChanged, this, &Editor::highlightCurrentLine);
highlightCurrentLine();
}
private:
void highlightCurrentLine(){
QList<QTextEdit::ExtraSelection> extraSelections;
if (!isReadOnly()) {
QTextEdit::ExtraSelection selection;
QColor lineColor("red");
selection.format.setBackground(lineColor);
selection.format.setProperty(QTextFormat::FullWidthSelection, true);
selection.cursor = textCursor();
selection.cursor.clearSelection();
extraSelections.append(selection);
}
setExtraSelections(extraSelections);
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
a.setStyle("fusion");
Editor editor;
editor.resize(640, 480);
editor.show();
return a.exec();
}

How to connect signal and slot with another object in qt --solved

I am doing a qt project, my goal is to draw something based on my input. The signal is in the main, and the drawing is in another object, I am not able to connect them.
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Frequency fre; //this is the class that does the drawing
QWidget *window = new QWidget;
QGridLayout *grid = new QGridLayout;
QGroupBox *groupBox = new QGroupBox(QObject::tr("Volume"));
QSpinBox *spinBox = new QSpinBox;
spinBox->setRange(0, 5);
QObject::connect(spinBox, SIGNAL(valueChanged(int)),&fre, SLOT(setVolume(int)));
QVBoxLayout *Vbox = new QVBoxLayout;
Vbox->addWidget(spinBox);
groupBox->setLayout(Vbox);
grid->addWidget(groupBox, 4,7,1,1);
window->setLayout(grid);
window->show();
return app.exec();
}
This is how I set up the Frequency class :
in h file
class Frequency : public QGLWidget
{
Q_OBJECT
public slots:
void setVolume(int value);
in cpp file
void Frequency :: setVolume(int val) {
vol = val;
updateGL();
}
void Frequency :: paintGL() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
draw();
}
Since I can't put all this in the comment section, I'm going to put it here:
I copied your code and I'm not able to reproduce the error with Qt version 5.14.0, because when I change the value in any way, the method, void MyObject::setVolume(int value) is being called.
This code should work for you. Possibly, you are running it in release mode, and used a breakpoint to check if it was called (which doesn't always work on release mode).
// #include "QtWidgetsApplication4.h"
#include <QtWidgets/QApplication>
#include "MyObject.h"
#include <QGridLayout>
#include <QGroupBox>
#include <QSpinBox>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// QtWidgetsApplication4 w;
// w.show();
// return a.exec();
MyObject object;
QWidget* window = new QWidget;
QGridLayout* grid = new QGridLayout;
QGroupBox* groupBox = new QGroupBox(QObject::tr("Volume"));
QSpinBox* spinBox = new QSpinBox;
spinBox->setRange(0, 5);
QObject::connect(spinBox, SIGNAL(valueChanged(int)), &object, SLOT(setVolume(int)));
QVBoxLayout* Vbox = new QVBoxLayout;
Vbox->addWidget(spinBox);
groupBox->setLayout(Vbox);
grid->addWidget(groupBox, 4, 7, 1, 1);
window->setLayout(grid);
window->show();
return a.exec();
}
#pragma once
#include <QObject>
class MyObject : public QObject
{
Q_OBJECT
public slots:
void setVolume(int value);
private:
int m_volume;
};
#include "MyObject.h"
void MyObject::setVolume(int value)
{
m_volume = value;
}

Qt signals/slots do not work [very basic] [duplicate]

This question already has answers here:
C++ Qt signal and slot not firing
(3 answers)
Closed 8 years ago.
I am very new to Qt and it looks like i have some very basic misunderstanding about how this library works. I am currently reading a book by m. Schlee but i do not want to continue until i do understand how to make this simple program work.
#include <QtWidgets>
#include <QApplication>
#include <QStackedWidget>
#include <QPushButton>
#include <QObject>
struct wizard : public QObject
{
QStackedWidget* p;
wizard(QStackedWidget* pp) : p(pp) { }
public slots:
void change()
{
int to = p->currentIndex();
if (to == p->count() - 1)
to = 0;
else
++to;
emit chIndex(to);
}
signals:
void chIndex(int);
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QStackedWidget qsw;
QPushButton qpb("magic");
qsw.resize(500, 500);
qsw.move(500, 300);
qsw.setWindowTitle("test qsw");
qpb.move(330, 300);
qpb.setWindowTitle("test qpb");
QWidget* pw1 = new QWidget();
QPalette pal1;
pal1.setColor(pw1->backgroundRole(), Qt::blue);
pw1->setPalette(pal1);
pw1->resize(500, 500);
pw1->setAutoFillBackground(true);
QWidget* pw2 = new QWidget();
QPalette pal2;
pal2.setColor(pw2->backgroundRole(), Qt::yellow);
pw2->setPalette(pal2);
pw2->resize(500, 500);
pw2->setAutoFillBackground(true);
qsw.addWidget(pw1);
qsw.addWidget(pw2);
wizard stupidity(&qsw);
QObject::connect(&qpb, SIGNAL(clicked()), &stupidity, SLOT(change()));
QObject::connect(&stupidity, SIGNAL(chIndex(int)), &qsw, SLOT(setCurrentIndex(int)));
qpb.show();
qsw.show();
return a.exec();
}
The idea is to launch 2 separate windows: one with painted background, and another with button that changes color (blue->yellow->blue->.. etc).
They appear, but nothing happens if i press the button. Please help.
Except for struct being a class and a missing Q_OBJECT macro the code is fine
Try the following:
create a main.h file having this content:
#ifndef MAIN_H
#define MAIN_H
#include <QObject>
#include <QStackedWidget>
class wizard : public QObject
{
Q_OBJECT
public:
QStackedWidget* p;
wizard(QStackedWidget* pp) : p(pp) { }
public slots:
void change()
{
int to = p->currentIndex();
if (to == p->count() - 1)
to = 0;
else
++to;
emit chIndex(to);
}
signals:
void chIndex(int);
};
#endif // MAIN_H
change your main.cpp to the following content:
#include <QtWidgets>
#include <QApplication>
#include <QPushButton>
#include <main.h>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QStackedWidget qsw;
QPushButton qpb("magic");
qsw.resize(500, 500);
qsw.move(500, 300);
qsw.setWindowTitle("test qsw");
qpb.move(330, 300);
qpb.setWindowTitle("test qpb");
QWidget* pw1 = new QWidget();
QPalette pal1;
pal1.setColor(pw1->backgroundRole(), Qt::blue);
pw1->setPalette(pal1);
pw1->resize(500, 500);
pw1->setAutoFillBackground(true);
QWidget* pw2 = new QWidget();
QPalette pal2;
pal2.setColor(pw2->backgroundRole(), Qt::yellow);
pw2->setPalette(pal2);
pw2->resize(500, 500);
pw2->setAutoFillBackground(true);
qsw.addWidget(pw1);
qsw.addWidget(pw2);
wizard stupidity(&qsw);
QObject::connect(&qpb, SIGNAL(clicked()), &stupidity, SLOT(change()));
QObject::connect(&stupidity, SIGNAL(chIndex(int)), &qsw, SLOT(setCurrentIndex(int)));
qpb.show();
qsw.show();
return a.exec();
}
qmake doesn't work very well with Q_OBJECT macro directly in cpp file. Btw. run qmake after changes applied.

Set icon to custom QFontDialog

I created my own font dialog from QFontDialog (new SLOT added). After that, I can't set icon (*.png) to my new font dialog (with func. setWindowIcon). If I use setWindowIcon to default QFontDialog - everything is OK. So, how to set icon to my new font dialog?
FontDialog.h
#include <QtGui>
#include <QtCore>
class FontDialog: public QFontDialog {
public:
FontDialog();
~FontDialog();
public slots:
void someSlot(void);
};
FontDialog.cpp
#include "FontDialog.h"
FontDialog::FontDialog() {
}
FontDialog::~FontDialog() {
}
void someSlot(void) {
}
main.cpp
#include "FontDialog.h"
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
FontDialog *fontDialog = new FontDialog();
fontDialog->setWindowIcon(QIcon(".//icon.png")); // !!!NOT WORKING!!! for custom QFontDialog
fontDialog->show();
app.exec();
}
I'm using Qt 4.8.5 with Qt Designer 2.7.1 environment.
Thanks for any help.
The below works under Qt 4.8.4 on both OS X and Windows 7. It also works on 5.1.1 on Windows, but not on OS X :(
#include <QFontDialog>
#include <QApplication>
#include <QIcon>
#include <QPainter>
class Dialog : public QFontDialog {
public:
Dialog(QWidget *parent = 0) : QFontDialog(parent) {}
Dialog(const QFont & initial, QWidget *parent = 0) : QFontDialog(initial, parent) {}
};
QIcon myIcon(const QColor & color)
{
QIcon icon;
QPixmap pm(128, 128);
pm.fill(Qt::transparent);
QPainter p(&pm);
p.translate(64, 64);
p.scale(50, 50);
p.setBrush(color);
p.setPen(QPen(Qt::lightGray, 0.1));
p.drawEllipse(-1, -1, 2, 2);
icon.addPixmap(pm);
return icon;
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
a.setWindowIcon(myIcon(Qt::red));
Dialog d;
d.setWindowIcon(myIcon(Qt::blue));
d.show();
return a.exec();
}

QWidget::heightForWidth() is not called

I want to make my widget always have square size. Following this answer, I have overridden QWidget::heightForWidth(), and I also call setHeightForWidth(true) in the constructor, as suggested by #peppe. The size policy is set to Preferred,Preferred (for both horizontal size and vertical size).
However, heightForWidth() is not being called. Is there anything I am doing wrong?
This is the declaration of heightForWidth() in my Widget class:
virtual int heightForWidth(int) const;
This happens on Linux and Windows.
Your widget needs to be in a layout. The below works on both Qt 4 and 5.
In Qt 4, it will only force the toplevel window's minimum size if it's in a layout.
In Qt 5, it doesn't force the toplevel window size. There may be a flag for that or it's a bug but I don't recall at the moment.
#include <QApplication>
#include <QWidget>
#include <QPainter>
#include <QDebug>
#include <QVBoxLayout>
#include <QFrame>
class Widget : public QWidget {
mutable int m_ctr;
public:
Widget(QWidget *parent = 0) : QWidget(parent), m_ctr(0) {
QSizePolicy p(sizePolicy());
p.setHeightForWidth(true);
setSizePolicy(p);
}
int heightForWidth(int width) const {
m_ctr ++;
QApplication::postEvent(const_cast<Widget*>(this), new QEvent(QEvent::UpdateRequest));
return qMax(width*2, 100);
}
QSize sizeHint() const {
return QSize(300, heightForWidth(300));
}
void paintEvent(QPaintEvent *) {
QPainter p(this);
p.drawRect(rect().adjusted(0, 0, -1, -1));
p.drawText(rect(), QString("h4w called %1 times").arg(m_ctr));
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget w;
QVBoxLayout * l = new QVBoxLayout(&w);
l->addWidget(new Widget);
QFrame * btm = new QFrame;
btm->setFrameShape(QFrame::Panel);
btm->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
l->addWidget(btm);
w.show();
return a.exec();
}
To stay square if the widget is in a layout you must reimplement
bool hasHeightForWidth() const{ return true; }
int heightForWidth(int w) const { return w; }
functions of the layout class.