I created connection in Designer, and then trying to change slot's functionality. And then i declare that private slot, then initialize it, and after build program is running but connection doesn't work. It says QObject::connect: No such slot QDialog
I think problem can be in ui_dl.h which is automaticly generated by Qt
dl.h
#ifndef DL_H
#define DL_H
#include <QDialog>
namespace Ui {
class Dl;
}
class Dl : public QDialog
{
Q_OBJECT
public:
explicit Dl(QWidget *parent = 0);
~Dl();
private:
Ui::Dl *ui;
private slots:
void print_shit();
};
#endif
dl.cpp
#include "dl.h"
#include "ui_dl.h"
#include <iostream>
Dl::Dl(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dl)
{
ui->setupUi(this);
}
Dl::~Dl()
{
delete ui;
}
void Dl::print_shit(){
}
main.cpp
#include <QApplication>
#include <QQmlApplicationEngine>
#include "ui_dl.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Ui_Dl * dialog = new Ui_Dl;
QDialog *wnd = new QDialog();
dialog->setupUi(wnd);
wnd->show();
return app.exec();
}
#ifndef UI_DL_H
#define UI_DL_H
#include <QtCore/QVariant>
#include <QtWidgets/QAction>
#include <QtWidgets/QApplication>
#include <QtWidgets/QButtonGroup>
#include <QtWidgets/QDialog>
#include <QtWidgets/QGridLayout>
#include <QtWidgets/QHBoxLayout>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QSlider>
#include <QtWidgets/QSpinBox>
#include <QtWidgets/QWidget>
QT_BEGIN_NAMESPACE
class Ui_Dl
{
public:
QWidget *layoutWidget;
QGridLayout *gridLayout;
QHBoxLayout *horizontalLayout_2;
QPushButton *pushButton_6;
QPushButton *knopka;
QHBoxLayout *horizontalLayout;
QPushButton *pushButton;
QPushButton *pushButton_2;
QPushButton *pushButton_3;
QHBoxLayout *horizontalLayout_3;
QPushButton *pushButton_8;
QPushButton *pushButton_7;
QPushButton *pushButton_9;
QSpinBox *spinBox;
QSlider *horizontalSlider;
void setupUi(QDialog *Dl)
{
if (Dl->objectName().isEmpty())
Dl->setObjectName(QStringLiteral("Dl"));
Dl->setWindowModality(Qt::NonModal);
Dl->resize(705, 504);
QSizePolicy sizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
sizePolicy.setHorizontalStretch(0);
sizePolicy.setVerticalStretch(0);
sizePolicy.setHeightForWidth(Dl->sizePolicy().hasHeightForWidth());
Dl->setSizePolicy(sizePolicy);
QPalette palette;
QBrush brush(QColor(0, 0, 0, 255));
brush.setStyle(Qt::SolidPattern);
palette.setBrush(QPalette::Active, QPalette::WindowText, brush);
QBrush brush1(QColor(153, 240, 185, 255));
brush1.setStyle(Qt::SolidPattern);
palette.setBrush(QPalette::Active, QPalette::Button, brush1);
QBrush brush2(QColor(255, 255, 255, 255));
brush2.setStyle(Qt::SolidPattern);
palette.setBrush(QPalette::Active, QPalette::Light, brush2);
QBrush brush3(QColor(204, 247, 220, 255));
brush3.setStyle(Qt::SolidPattern);
palette.setBrush(QPalette::Active, QPalette::Midlight, brush3);
QBrush brush4(QColor(76, 120, 92, 255));
brush4.setStyle(Qt::SolidPattern);
palette.setBrush(QPalette::Active, QPalette::Dark, brush4);
QBrush brush5(QColor(102, 160, 123, 255));
brush5.setStyle(Qt::SolidPattern);
palette.setBrush(QPalette::Active, QPalette::Mid, brush5);
palette.setBrush(QPalette::Active, QPalette::Text, brush);
palette.setBrush(QPalette::Active, QPalette::BrightText, brush2);
palette.setBrush(QPalette::Active, QPalette::ButtonText, brush);
palette.setBrush(QPalette::Active, QPalette::Base, brush2);
palette.setBrush(QPalette::Active, QPalette::Window, brush1);
palette.setBrush(QPalette::Active, QPalette::Shadow, brush);
palette.setBrush(QPalette::Active, QPalette::AlternateBase, brush3);
QBrush brush6(QColor(255, 255, 220, 255));
brush6.setStyle(Qt::SolidPattern);
palette.setBrush(QPalette::Active, QPalette::ToolTipBase, brush6);
palette.setBrush(QPalette::Active, QPalette::ToolTipText, brush);
palette.setBrush(QPalette::Inactive, QPalette::WindowText, brush);
palette.setBrush(QPalette::Inactive, QPalette::Button, brush1);
palette.setBrush(QPalette::Inactive, QPalette::Light, brush2);
palette.setBrush(QPalette::Inactive, QPalette::Midlight, brush3);
palette.setBrush(QPalette::Inactive, QPalette::Dark, brush4);
palette.setBrush(QPalette::Inactive, QPalette::Mid, brush5);
palette.setBrush(QPalette::Inactive, QPalette::Text, brush);
palette.setBrush(QPalette::Inactive, QPalette::BrightText, brush2);
palette.setBrush(QPalette::Inactive, QPalette::ButtonText, brush);
palette.setBrush(QPalette::Inactive, QPalette::Base, brush2);
palette.setBrush(QPalette::Inactive, QPalette::Window, brush1);
palette.setBrush(QPalette::Inactive, QPalette::Shadow, brush);
palette.setBrush(QPalette::Inactive, QPalette::AlternateBase, brush3);
palette.setBrush(QPalette::Inactive, QPalette::ToolTipBase, brush6);
palette.setBrush(QPalette::Inactive, QPalette::ToolTipText, brush);
palette.setBrush(QPalette::Disabled, QPalette::WindowText, brush4);
palette.setBrush(QPalette::Disabled, QPalette::Button, brush1);
palette.setBrush(QPalette::Disabled, QPalette::Light, brush2);
palette.setBrush(QPalette::Disabled, QPalette::Midlight, brush3);
palette.setBrush(QPalette::Disabled, QPalette::Dark, brush4);
palette.setBrush(QPalette::Disabled, QPalette::Mid, brush5);
palette.setBrush(QPalette::Disabled, QPalette::Text, brush4);
palette.setBrush(QPalette::Disabled, QPalette::BrightText, brush2);
palette.setBrush(QPalette::Disabled, QPalette::ButtonText, brush4);
palette.setBrush(QPalette::Disabled, QPalette::Base, brush1);
palette.setBrush(QPalette::Disabled, QPalette::Window, brush1);
palette.setBrush(QPalette::Disabled, QPalette::Shadow, brush);
palette.setBrush(QPalette::Disabled, QPalette::AlternateBase, brush1);
palette.setBrush(QPalette::Disabled, QPalette::ToolTipBase, brush6);
palette.setBrush(QPalette::Disabled, QPalette::ToolTipText, brush);
Dl->setPalette(palette);
Dl->setCursor(QCursor(Qt::ArrowCursor));
Dl->setAutoFillBackground(false);
Dl->setSizeGripEnabled(true);
layoutWidget = new QWidget(Dl);
layoutWidget->setObjectName(QStringLiteral("layoutWidget"));
layoutWidget->setGeometry(QRect(140, 90, 421, 351));
gridLayout = new QGridLayout(layoutWidget);
gridLayout->setObjectName(QStringLiteral("gridLayout"));
gridLayout->setContentsMargins(0, 0, 0, 0);
horizontalLayout_2 = new QHBoxLayout();
horizontalLayout_2->setObjectName(QStringLiteral("horizontalLayout_2"));
horizontalLayout_2->setSizeConstraint(QLayout::SetNoConstraint);
pushButton_6 = new QPushButton(layoutWidget);
pushButton_6->setObjectName(QStringLiteral("pushButton_6"));
sizePolicy.setHeightForWidth(pushButton_6->sizePolicy().hasHeightForWidth());
pushButton_6->setSizePolicy(sizePolicy);
pushButton_6->setMaximumSize(QSize(191, 103));
pushButton_6->setMouseTracking(false);
pushButton_6->setAcceptDrops(false);
pushButton_6->setAutoFillBackground(false);
pushButton_6->setStyleSheet(QStringLiteral("border-image: url(:/new/prefix1/lico);"));
pushButton_6->setCheckable(false);
pushButton_6->setAutoRepeat(false);
pushButton_6->setAutoExclusive(false);
pushButton_6->setAutoDefault(true);
pushButton_6->setFlat(false);
horizontalLayout_2->addWidget(pushButton_6);
knopka = new QPushButton(layoutWidget);
knopka->setObjectName(QStringLiteral("knopka"));
knopka->setEnabled(true);
sizePolicy.setHeightForWidth(knopka->sizePolicy().hasHeightForWidth());
knopka->setSizePolicy(sizePolicy);
knopka->setSizeIncrement(QSize(0, 0));
knopka->setMouseTracking(false);
knopka->setAutoFillBackground(false);
knopka->setStyleSheet(QStringLiteral(""));
knopka->setCheckable(false);
knopka->setAutoDefault(true);
knopka->setFlat(false);
horizontalLayout_2->addWidget(knopka);
gridLayout->addLayout(horizontalLayout_2, 1, 0, 1, 1);
horizontalLayout = new QHBoxLayout();
horizontalLayout->setObjectName(QStringLiteral("horizontalLayout"));
horizontalLayout->setSizeConstraint(QLayout::SetNoConstraint);
pushButton = new QPushButton(layoutWidget);
pushButton->setObjectName(QStringLiteral("pushButton"));
QSizePolicy sizePolicy1(QSizePolicy::Minimum, QSizePolicy::Preferred);
sizePolicy1.setHorizontalStretch(0);
sizePolicy1.setVerticalStretch(0);
sizePolicy1.setHeightForWidth(pushButton->sizePolicy().hasHeightForWidth());
pushButton->setSizePolicy(sizePolicy1);
pushButton->setMouseTracking(false);
pushButton->setCheckable(false);
horizontalLayout->addWidget(pushButton);
pushButton_2 = new QPushButton(layoutWidget);
pushButton_2->setObjectName(QStringLiteral("pushButton_2"));
sizePolicy1.setHeightForWidth(pushButton_2->sizePolicy().hasHeightForWidth());
pushButton_2->setSizePolicy(sizePolicy1);
pushButton_2->setMinimumSize(QSize(125, 104));
pushButton_2->setMouseTracking(false);
pushButton_2->setCheckable(false);
horizontalLayout->addWidget(pushButton_2);
pushButton_3 = new QPushButton(layoutWidget);
pushButton_3->setObjectName(QStringLiteral("pushButton_3"));
sizePolicy.setHeightForWidth(pushButton_3->sizePolicy().hasHeightForWidth());
pushButton_3->setSizePolicy(sizePolicy);
pushButton_3->setMouseTracking(false);
pushButton_3->setCheckable(false);
horizontalLayout->addWidget(pushButton_3);
gridLayout->addLayout(horizontalLayout, 0, 0, 1, 1);
horizontalLayout_3 = new QHBoxLayout();
horizontalLayout_3->setObjectName(QStringLiteral("horizontalLayout_3"));
horizontalLayout_3->setSizeConstraint(QLayout::SetNoConstraint);
pushButton_8 = new QPushButton(layoutWidget);
pushButton_8->setObjectName(QStringLiteral("pushButton_8"));
sizePolicy1.setHeightForWidth(pushButton_8->sizePolicy().hasHeightForWidth());
pushButton_8->setSizePolicy(sizePolicy1);
pushButton_8->setMouseTracking(false);
pushButton_8->setStyleSheet(QString::fromUtf8("border-image: url(C:/Users/Acer/Pictures/Screenshot \320\273\320\270\321\206\320\276(\320\260\320\262\320\260\321\202\320\260\321\200).png);"));
pushButton_8->setCheckable(false);
horizontalLayout_3->addWidget(pushButton_8);
pushButton_7 = new QPushButton(layoutWidget);
pushButton_7->setObjectName(QStringLiteral("pushButton_7"));
sizePolicy1.setHeightForWidth(pushButton_7->sizePolicy().hasHeightForWidth());
pushButton_7->setSizePolicy(sizePolicy1);
pushButton_7->setMouseTracking(false);
pushButton_7->setStyleSheet(QStringLiteral("border-image: url(:/new/prefix1/lico);"));
pushButton_7->setCheckable(false);
horizontalLayout_3->addWidget(pushButton_7);
pushButton_9 = new QPushButton(layoutWidget);
pushButton_9->setObjectName(QStringLiteral("pushButton_9"));
sizePolicy.setHeightForWidth(pushButton_9->sizePolicy().hasHeightForWidth());
pushButton_9->setSizePolicy(sizePolicy);
pushButton_9->setMouseTracking(false);
pushButton_9->setCheckable(false);
horizontalLayout_3->addWidget(pushButton_9);
gridLayout->addLayout(horizontalLayout_3, 2, 0, 1, 1);
spinBox = new QSpinBox(Dl);
spinBox->setObjectName(QStringLiteral("spinBox"));
spinBox->setGeometry(QRect(590, 250, 101, 121));
horizontalSlider = new QSlider(Dl);
horizontalSlider->setObjectName(QStringLiteral("horizontalSlider"));
horizontalSlider->setGeometry(QRect(470, 470, 160, 22));
horizontalSlider->setOrientation(Qt::Horizontal);
retranslateUi(Dl);
QObject::connect(knopka, SIGNAL(clicked()), Dl, SLOT(print_shit()));
QMetaObject::connectSlotsByName(Dl);
} // setupUi
void retranslateUi(QDialog *Dl)
{
Dl->setWindowTitle(QApplication::translate("Dl", "Dialog", 0));
pushButton_6->setText(QApplication::translate("Dl", "\320\243\320\272\321\200\320\260\320\270\320\275\320\260", 0));
knopka->setText(QApplication::translate("Dl", "\320\234\320\270\321\201\321\202\320\265\321\200 \320\264\321\203\320\264\320\265\321\206", 0));
pushButton->setText(QApplication::translate("Dl", "\320\257 \320\265\320\261\321\203 \321\201\320\276\320\261\320\260\320\272", 0));
#ifndef QT_NO_TOOLTIP
pushButton_2->setToolTip(QApplication::translate("Dl", "<html><head/><body><p><br/></p></body></html>", 0));
#endif // QT_NO_TOOLTIP
pushButton_2->setText(QApplication::translate("Dl", "\320\242\321\200\320\260\320\274\320\277", 0));
pushButton_3->setText(QApplication::translate("Dl", "\320\234\320\270\321\201\321\202\321\200\320\265 \320\264\321\203\320\264\320\265\321\206", 0));
pushButton_8->setText(QApplication::translate("Dl", "\320\242\321\200\320\260\320\274\320\277", 0));
pushButton_7->setText(QApplication::translate("Dl", "\320\243\320\272\321\200\320\260\320\270\320\275\320\260", 0));
pushButton_9->setText(QApplication::translate("Dl", "\320\257 \320\265\320\261\321\203 \321\201\320\276\320\261\320\260\320\272", 0));
} // retranslateUi
};
namespace Ui {
class Dl: public Ui_Dl {};
} // namespace Ui
QT_END_NAMESPACE
#endif // UI_DL_H
I see two possible problems:
You pass *Dl as QDialog into SetupUI:
void setupUi(QDialog *Dl) {
…
}
But your slot is defined in your custom class Dl. So Qt says a QDialog does not have a slot called print_shit().
Additionally connectSlotsByName requires your slots to follow a certain naming convention:
void QMetaObject::connectSlotsByName(QObject *object)
Searches recursively for all child objects of the given object, and connects matching signals from them to slots of object that follow the following form:
void on_<object name>_<signal name>(<signal parameters>);
Let's assume our object has a child object of type QPushButton with the object name button1. The slot to catch the button's clicked() signal would be:
void on_button1_clicked();
You don’t seem to have such slots in your class Dl.
Related
I'm trying to get the behavior that when I drag and drop a MyButton, the dropEvent will be triggered in any part of the interface. Right now it works pretty much as needed, except for one thing. The dropEvent doesn't work if the mouse button is released between widgets, and I don't know how to fix this problem. For me it is very important that the dropEvent is always triggered, at the moment when the drag is finished, because at that moment I send a message to the server about the move made, and if at that moment the mouse was between the widgets, this message is not sent.
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'DS_Test.ui'
#
# Created by: PyQt5 UI code generator 5.15.7
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QDragEnterEvent, QDropEvent
class MyButton(QtWidgets.QPushButton):
def mouseMoveEvent(self, event):
btn_img = self.grab()
painter = QtGui.QPainter(btn_img)
painter.setCompositionMode(painter.CompositionMode_DestinationIn)
painter.fillRect(btn_img.rect(), QtGui.QColor(0, 0, 0, 150))
painter.end()
data = QtCore.QMimeData()
drag = QtGui.QDrag(self)
drag.setMimeData(data)
drag.setPixmap(btn_img)
drag.setHotSpot(event.pos())
self._pos = event.pos()
drag.exec_(QtCore.Qt.CopyAction)
super(MyButton, self).mouseMoveEvent(event)
class MyFrame(QtWidgets.QFrame):
def __init__(self, parent):
super().__init__(parent)
self.setAcceptDrops(True)
def dragEnterEvent(self, e: QDragEnterEvent):
e.accept()
def dragMoveEvent(self, e):
e.accept()
def dropEvent(self, e: QDropEvent):
self.parent().dropEvent(e)
class MyQDialog(QtWidgets.QDialog):
def __init__(self):
super().__init__()
self.setAcceptDrops(True)
def dropEvent(self, e: QDropEvent):
print("dropEvent - is done")
e.accept()
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(1051, 800)
self.verticalLayout = QtWidgets.QVBoxLayout(Dialog)
self.verticalLayout.setObjectName("verticalLayout")
self.horizontalFrame = MyFrame(Dialog)
self.horizontalFrame.setStyleSheet("background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(255, 0, 0, 211), stop:0.166 rgba(255, 255, 0, 211), stop:0.333 rgba(0, 255, 0, 211), stop:0.5 rgba(0, 255, 255, 211), stop:0.666 rgba(0, 0, 255, 211), stop:0.833 rgba(255, 0, 255, 211), stop:1 rgba(255, 0, 0, 211));")
self.horizontalFrame.setObjectName("horizontalFrame")
self.horizontalLayout_6 = QtWidgets.QHBoxLayout(self.horizontalFrame)
self.horizontalLayout_6.setObjectName("horizontalLayout_6")
self.pushButton_3 = MyButton(self.horizontalFrame)
self.pushButton_3.setObjectName("pushButton_3")
self.horizontalLayout_6.addWidget(self.pushButton_3)
self.pushButton_2 = MyButton(self.horizontalFrame)
self.pushButton_2.setObjectName("pushButton_2")
self.horizontalLayout_6.addWidget(self.pushButton_2)
self.pushButton = MyButton(self.horizontalFrame)
self.pushButton.setObjectName("pushButton")
self.horizontalLayout_6.addWidget(self.pushButton)
self.verticalLayout.addWidget(self.horizontalFrame)
self.horizontalFrame_2 = MyFrame(Dialog)
self.horizontalFrame_2.setStyleSheet("background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(9, 41, 4, 255), stop:0.085 rgba(2, 79, 0, 255), stop:0.19 rgba(50, 147, 22, 255), stop:0.275 rgba(236, 191, 49, 255), stop:0.39 rgba(243, 61, 34, 255), stop:0.555 rgba(135, 81, 60, 255), stop:0.667 rgba(121, 75, 255, 255), stop:0.825 rgba(164, 255, 244, 255), stop:0.885 rgba(104, 222, 71, 255), stop:1 rgba(93, 128, 0, 255));")
self.horizontalFrame_2.setObjectName("horizontalFrame_2")
self.horizontalLayout_7 = QtWidgets.QHBoxLayout(self.horizontalFrame_2)
self.horizontalLayout_7.setObjectName("horizontalLayout_7")
self.verticalLayout.addWidget(self.horizontalFrame_2)
self.horizontalFrame_3 = MyFrame(Dialog)
self.horizontalFrame_3.setStyleSheet("background-color: qlineargradient(spread:pad, x1:0, y1:1, x2:0, y2:0, stop:0 rgba(0, 0, 0, 255), stop:0.05 rgba(14, 8, 73, 255), stop:0.36 rgba(28, 17, 145, 255), stop:0.6 rgba(126, 14, 81, 255), stop:0.75 rgba(234, 11, 11, 255), stop:0.79 rgba(244, 70, 5, 255), stop:0.86 rgba(255, 136, 0, 255), stop:0.935 rgba(239, 236, 55, 255));")
self.horizontalFrame_3.setObjectName("horizontalFrame_3")
self.horizontalLayout_8 = QtWidgets.QHBoxLayout(self.horizontalFrame_3)
self.horizontalLayout_8.setObjectName("horizontalLayout_8")
self.verticalLayout.addWidget(self.horizontalFrame_3)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.pushButton_3.setText(_translate("Dialog", "PushButton"))
self.pushButton_2.setText(_translate("Dialog", "PushButton"))
self.pushButton.setText(_translate("Dialog", "PushButton"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = MyQDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
As recommended in the comment, I added dragEnterEvent and dragMoveEvent to MyQDialog. Now everything works the way I want it to. Here is the corrected code
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'DS_Test.ui'
#
# Created by: PyQt5 UI code generator 5.15.7
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QDragEnterEvent, QDropEvent
class MyButton(QtWidgets.QPushButton):
def mouseMoveEvent(self, event):
btn_img = self.grab()
painter = QtGui.QPainter(btn_img)
painter.setCompositionMode(painter.CompositionMode_DestinationIn)
painter.fillRect(btn_img.rect(), QtGui.QColor(0, 0, 0, 150))
painter.end()
data = QtCore.QMimeData()
drag = QtGui.QDrag(self)
drag.setMimeData(data)
drag.setPixmap(btn_img)
drag.setHotSpot(event.pos())
self._pos = event.pos()
drag.exec_(QtCore.Qt.CopyAction)
super(MyButton, self).mouseMoveEvent(event)
class MyFrame(QtWidgets.QFrame):
def __init__(self, parent):
super().__init__(parent)
self.setAcceptDrops(True)
def dragEnterEvent(self, e: QDragEnterEvent):
e.accept()
def dragMoveEvent(self, e):
e.accept()
def dropEvent(self, e: QDropEvent):
self.parent().dropEvent(e)
class MyQDialog(QtWidgets.QDialog):
def __init__(self):
super().__init__()
self.setAcceptDrops(True)
def dragEnterEvent(self, e: QDragEnterEvent):
e.accept()
def dragMoveEvent(self, e):
e.accept()
def dropEvent(self, e: QDropEvent):
print("dropEvent - is done")
e.accept()
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(1051, 800)
self.verticalLayout = QtWidgets.QVBoxLayout(Dialog)
self.verticalLayout.setObjectName("verticalLayout")
self.horizontalFrame = MyFrame(Dialog)
self.horizontalFrame.setStyleSheet("background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(255, 0, 0, 211), stop:0.166 rgba(255, 255, 0, 211), stop:0.333 rgba(0, 255, 0, 211), stop:0.5 rgba(0, 255, 255, 211), stop:0.666 rgba(0, 0, 255, 211), stop:0.833 rgba(255, 0, 255, 211), stop:1 rgba(255, 0, 0, 211));")
self.horizontalFrame.setObjectName("horizontalFrame")
self.horizontalLayout_6 = QtWidgets.QHBoxLayout(self.horizontalFrame)
self.horizontalLayout_6.setObjectName("horizontalLayout_6")
self.pushButton_3 = MyButton(self.horizontalFrame)
self.pushButton_3.setObjectName("pushButton_3")
self.horizontalLayout_6.addWidget(self.pushButton_3)
self.pushButton_2 = MyButton(self.horizontalFrame)
self.pushButton_2.setObjectName("pushButton_2")
self.horizontalLayout_6.addWidget(self.pushButton_2)
self.pushButton = MyButton(self.horizontalFrame)
self.pushButton.setObjectName("pushButton")
self.horizontalLayout_6.addWidget(self.pushButton)
self.verticalLayout.addWidget(self.horizontalFrame)
self.horizontalFrame_2 = MyFrame(Dialog)
self.horizontalFrame_2.setStyleSheet("background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(9, 41, 4, 255), stop:0.085 rgba(2, 79, 0, 255), stop:0.19 rgba(50, 147, 22, 255), stop:0.275 rgba(236, 191, 49, 255), stop:0.39 rgba(243, 61, 34, 255), stop:0.555 rgba(135, 81, 60, 255), stop:0.667 rgba(121, 75, 255, 255), stop:0.825 rgba(164, 255, 244, 255), stop:0.885 rgba(104, 222, 71, 255), stop:1 rgba(93, 128, 0, 255));")
self.horizontalFrame_2.setObjectName("horizontalFrame_2")
self.horizontalLayout_7 = QtWidgets.QHBoxLayout(self.horizontalFrame_2)
self.horizontalLayout_7.setObjectName("horizontalLayout_7")
self.verticalLayout.addWidget(self.horizontalFrame_2)
self.horizontalFrame_3 = MyFrame(Dialog)
self.horizontalFrame_3.setStyleSheet("background-color: qlineargradient(spread:pad, x1:0, y1:1, x2:0, y2:0, stop:0 rgba(0, 0, 0, 255), stop:0.05 rgba(14, 8, 73, 255), stop:0.36 rgba(28, 17, 145, 255), stop:0.6 rgba(126, 14, 81, 255), stop:0.75 rgba(234, 11, 11, 255), stop:0.79 rgba(244, 70, 5, 255), stop:0.86 rgba(255, 136, 0, 255), stop:0.935 rgba(239, 236, 55, 255));")
self.horizontalFrame_3.setObjectName("horizontalFrame_3")
self.horizontalLayout_8 = QtWidgets.QHBoxLayout(self.horizontalFrame_3)
self.horizontalLayout_8.setObjectName("horizontalLayout_8")
self.verticalLayout.addWidget(self.horizontalFrame_3)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.pushButton_3.setText(_translate("Dialog", "PushButton"))
self.pushButton_2.setText(_translate("Dialog", "PushButton"))
self.pushButton.setText(_translate("Dialog", "PushButton"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = MyQDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
I was doing this by creating QFrame() object in loop and it add in QFormLayout to get multiple Frame but it shows nothing .
scr = QScrollArea()
frame = QFrame()
frame.setStyleSheet("background:'black'")
lbl= QLabel(f"{name_playlist[details]}",frame)
lbl.setStyleSheet('''background-color: qradialgradient(spread:pad, cx:0.5, cy:0.5, radius:1.772, fx:0.5, fy:0.5, stop:0 rgba(216, 106, 126, 255), stop:1 rgba(255, 255, 255, 255));
color:#fff;
font: 63 12pt "Bahnschrift SemiBold";border-radius:10px''')
lbl.setGeometry(QRect(0,170,201,31))
lbl_1 = QLabel(frame)
pixmap = QPixmap(f"{thumbnails_playlist[details]}")
pixmap= pixmap.scaled(191,161)
lbl_1.setStyleSheet("border-radius: 15px")
lbl_1.setPixmap(pixmap)
# nm_songs.append(lbl)
thumbnail.append(frame)
formlayout.addRow(thumbnail[details])
groupbox.setLayout(formlayout)
self.scroll.setWidget(groupbox)```
I want to make the white rectangles move across the screen like traffic and stop when the light is red. I have started this on the move method, totally new to processing js.
Currently the cars are the correct distance apart but i am unsure how to move thtis loop and move across the screen until the traiff light is red.
let counter = 0;
let lightorder = 0;
//setup the enviroment i.e. roads, lines on roads and trees
void setup() {
size(1330, 720);
background(96, 153, 25);
//Roads
noStroke();
fill(100);
rect(0, 200 + 10, 1330, 300);
rect(520, 0, 300, 1500);
//Lines on road
fill(255);
rect(0, 340, 90, 30);
rect(200, 340, 90, 30);
rect(400, 340, 90, 30);
rect(860, 340, 90, 30);
rect(1060, 340, 90, 30);
rect(1260, 340, 90, 30);
rect(655, 75, 30, 90);
rect(655, 575, 30, 90);
for(int i=0; i < 10; i++){
move(i = i +20)
}
}
//work out light order and assign and fill shapes on traffic lights according
void draw() {
determineLightOrder();
trafficLight();
trafficLight2();
trafficLight3();
trafficLight4();
}
void move(int p){
rect(p, 230, 200, 70);
rect(p, 400, 200, 70);
}
//determine if traffic light is red, yellow or green and fill shape
accordingly
void trafficLight() {
let is_red_light = lightorder == 2;
let is_yellow_light = lightorder == 1;
let is_green_light = lightorder == 0;
stroke(250);
fill(0);
rect(10 + 550, 10 + 80, 40, 100);
if (is_red_light) {
fill(255, 0, 0);
}
else {
fill(100);
}
ellipse(30 + 550, 30 + 80, 20, 20);
if (is_yellow_light) {
fill(250, 250, 0);
ellipse(30 + 550, 60 + 80, 20, 20);
}
// green ligh
if (is_green_light) {
fill(0, 255, 0);
ellipse(30 + 550, 90 + 80, 20, 20);
}
}
void trafficLight2() {
//declare light color variables
let is_red_light = lightorder == 0;
let is_yellow_light = lightorder == 2;
let is_green_light = lightorder == 1;
stroke(250);
fill(0);
rect(10 + 850, 10 + 215, 40, 100);
if (is_red_light) {
fill(255, 0, 0);
}
else {
fill(100);
}
ellipse(30 + 850, 30 + 215, 20, 20);
if (is_yellow_light) {
fill(250, 250, 0);
ellipse(30 + 850, 60 + 215, 20, 20);
}
if (is_green_light) {
fill(0, 255, 0);
ellipse(30 + 850, 90 + 215, 20, 20);
}
}
//same code as the trafficLight2 different position of lights
void trafficLight3() {
stroke(250);
fill(0);
rect(10 + 420, 10 + 380, 40, 100);
let is_red_light = lightorder == 0;
let is_yellow_light = lightorder == 2;
let is_green_light = lightorder == 1;
if (is_red_light) {
fill(255, 0, 0);
}
else {
fill(100);
}
ellipse(30 + 420, 30 + 380, 20, 20);
if (is_yellow_light) {
fill(250, 250, 0);
ellipse(30 + 420, 60 + 380, 20, 20);
}
if (is_green_light) {
fill(0, 255, 0);
ellipse(30 + 420, 90 + 380, 20, 20);
}
}
//same code as trafficLight1 different position of lights
void trafficLight4() {
let is_red_light = lightorder == 2;
let is_yellow_light = lightorder == 1;
let is_green_light = lightorder == 0;
stroke(250);
fill(0);
rect(10 + 730, 10 + 525, 40, 100);
if (is_red_light) {
fill(255, 0, 0);
}
else {
fill(100);
}
ellipse(30 + 730, 30 + 525, 20, 20);
if (is_yellow_light) {
fill(250, 250, 0);
ellipse(30 + 730, 60 + 525, 20, 20);
}
if (is_green_light) {
fill(0, 255, 0);
ellipse(30 + 730, 90 + 525, 20, 20);
}
}
//determine the light order
void determineLightOrder() {
counter++;
if (counter == 200) {
counter = 0;
lightorder = 0;
}
else if (counter == 50) {
lightorder = 1;
}
else if (counter == 70) {
lightorder = 2;
}
}
Stack Overflow isn't really designed for general "how do I do this" type questions. It's for specific "I tried X, expected Y, but got Z instead" type questions. But I'll try to help in a general sense:
I wrote this tutorial on animation in Processing that I highly recommend you read. But basically, you need to store the state of your program (for example, the position of the rectangles) in a variable (or multiple variables). Use those variables to draw your scene, and then change those variables over time to create an animation.
Break your problem down into smaller pieces and take those pieces on one at a time. Can you create a simple program that shows a single moving square? Then get that square to stop given a certain criteria. Then add a second square. Keep working your way forward in small steps like that, and if you get stuck on a specific step, post a MCVE along with a more specific question. Good luck.
I am trying to write Swift implementation of the following ObjC(header file) code.
#include <stddef.h>
#ifndef VO_CERTIFICATE_TYPE
#define VO_CERTIFICATE_TYPE
typedef struct _voCertificate
{
const char* bytes;
size_t length;
}
voCertificate;
#endif
static const char myCertificate_BYTES[] =
{
103, 92, -99, 33, 72, 48, 119, -72,
-77, 75, -88, 81, 113, -46, -119, -119,
5, 42, -33, 94, 23, 3, -112, 34,
-63, 75, -77, 26, -41, -69, 50, 71,
19, 121, 109, -60, 40, 18, 46, -86,
..........
};
voCertificate const myCertificate =
{
myCertificate_BYTES,
sizeof(myCertificate_BYTES)
};
//////////////////////////////////////
NSData *certificate = [NSData dataWithBytes:myCertificate.bytes length:myCertificate.length];
My best assumption was:
let myCertificate = [
103, 92, -99, 33, 72, 48, 119, -72,
-77, 75, -88, 81, 113, -46, -119, -119,
5, 42, -33, 94, 23, 3, -112, 34,
-63, 75, -77, 26, -41, -69, 50, 71,
19, 121, 109, -60, 40, 18, 46, -86,
........................]
var certificate = NSData(bytes: myCertificate as [Byte], length: myCertificate.count)
I tried to reach ObjC variable through Bridging-Header too, but there was "Undefined symbols for architecture armv7" error.
I would really appreciate any help.
Your biggest problem is that the type of your myCertificate array is Int not Int8. Here is something that is working for me. Note I reconstructed the array from the NSData object to see if everything came out ok.
let myCertificate = Array<Int8>(arrayLiteral:
103, 92, -99, 33, 72, 48, 119, -72,
-77, 75, -88, 81, 113, -46, -119, -119,
5, 42, -33, 94, 23, 3, -112, 34,
-63, 75, -77, 26, -41, -69, 50, 71,
19, 121, 109, -60, 40, 18, 46, -86)
var certificate = NSData(bytes: myCertificate, length: myCertificate.count)
var buffer = [Int8](count: certificate.length, repeatedValue: 0)
certificate.getBytes(&buffer, length: certificate.length)
I'm writing a simple texture stream rendering program using OpenGL ES 2.0. The program works on desktop but fail on embedded platform with Mali400 GPU. The LCD goes black with the top few lines blinking. I don't know what's wrong with my code. I tried some other OpenGL ES 2.0 programs which are OK, so the problem must lay in my code. Any help will be appreciated. Thanks.
main.c
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <EGL/egl.h>
#include <GLES2/gl2.h>
#include <sys/stat.h>
static EGLDisplay display;
static EGLSurface surface;
static void render_target_init(EGLNativeWindowType nativeWindow)
{
assert((display = eglGetDisplay(EGL_DEFAULT_DISPLAY)) != EGL_NO_DISPLAY);
EGLint majorVersion;
EGLint minorVersion;
assert(eglInitialize(display, &majorVersion, &minorVersion) == EGL_TRUE);
EGLConfig config;
EGLint numConfigs;
const EGLint configAttribs[] = {
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_RED_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_BLUE_SIZE, 8,
EGL_DEPTH_SIZE, 24,
EGL_NONE
};
assert(eglChooseConfig(display, configAttribs, &config, 1, &numConfigs) == EGL_TRUE);
const EGLint attribList[] = {
EGL_RENDER_BUFFER, EGL_BACK_BUFFER,
EGL_NONE
};
assert((surface = eglCreateWindowSurface(display, config, nativeWindow, attribList)) != EGL_NO_SURFACE);
EGLContext context;
const EGLint contextAttribs[] = {
EGL_CONTEXT_CLIENT_VERSION, 2,
EGL_NONE
};
assert((context = eglCreateContext(display, config, EGL_NO_CONTEXT, contextAttribs)) != EGL_NO_CONTEXT);
assert(eglMakeCurrent(display, surface, surface, context) == EGL_TRUE);
}
static GLuint LoadShader(const char *name, GLenum type)
{
FILE *f;
int size;
char *buff;
GLuint shader;
GLint compiled;
const char *source[1];
assert((f = fopen(name, "r")) != NULL);
// get file size
fseek(f, 0, SEEK_END);
size = ftell(f);
fseek(f, 0, SEEK_SET);
assert((buff = malloc(size)) != NULL);
assert(fread(buff, 1, size, f) == size);
source[0] = buff;
fclose(f);
shader = glCreateShader(type);
glShaderSource(shader, 1, source, &size);
glCompileShader(shader);
free(buff);
glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
if (!compiled) {
GLint infoLen = 0;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
if (infoLen > 1) {
char *infoLog = malloc(infoLen);
glGetShaderInfoLog(shader, infoLen, NULL, infoLog);
fprintf(stderr, "Error compiling shader %s:\n%s\n", name, infoLog);
free(infoLog);
}
glDeleteShader(shader);
return 0;
}
return shader;
}
static void init_GLES(int width, int height)
{
GLint linked;
GLuint program;
GLuint vertexShader;
GLuint fragmentShader;
assert((vertexShader = LoadShader("vert.glsl", GL_VERTEX_SHADER)) != 0);
assert((fragmentShader = LoadShader("frag.glsl", GL_FRAGMENT_SHADER)) != 0);
assert((program = glCreateProgram()) != 0);
glAttachShader(program, vertexShader);
glAttachShader(program, fragmentShader);
glLinkProgram(program);
glGetProgramiv(program, GL_LINK_STATUS, &linked);
if (!linked) {
GLint infoLen = 0;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infoLen);
if (infoLen > 1) {
char *infoLog = malloc(infoLen);
glGetProgramInfoLog(program, infoLen, NULL, infoLog);
fprintf(stderr, "Error linking program:\n%s\n", infoLog);
free(infoLog);
}
glDeleteProgram(program);
exit(1);
}
glClearColor(0.15f, 0.15f, 0.15f, 0.15f);
glViewport(0, 0, width, height);
glEnable(GL_DEPTH_TEST);
glUseProgram(program);
GLfloat vertex[] = {
1, 0, 0,
0, 1, 0,
-1, 0, 0,
0, -1, 0,
};
GLfloat texcoord[] = {
1, 1,
0, 1,
0, 0,
1, 0,
};
GLushort index[] = {
0, 1, 2,
0, 3, 2,
};
GLuint VBO[3];
glGenBuffers(3, VBO);
GLint pos = glGetAttribLocation(program, "positionIn");
glBindBuffer(GL_ARRAY_BUFFER, VBO[0]);
glBufferData(GL_ARRAY_BUFFER, 4 * sizeof(GLfloat) * 3, vertex, GL_STATIC_DRAW);
glEnableVertexAttribArray(pos);
glVertexAttribPointer(pos, 3, GL_FLOAT, 0, 0, 0);
GLint tex = glGetAttribLocation(program, "texcoordIn");
glBindBuffer(GL_ARRAY_BUFFER, VBO[1]);
glBufferData(GL_ARRAY_BUFFER, 4 * sizeof(GLfloat) * 2, texcoord, GL_STATIC_DRAW);
glEnableVertexAttribArray(tex);
glVertexAttribPointer(tex, 2, GL_FLOAT, 0, 0, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, VBO[2]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6 * sizeof(GLushort), index, GL_STATIC_DRAW);
GLuint texid;
GLint texMap = glGetUniformLocation(program, "texMap");
glUniform1i(texMap, 0); // GL_TEXTURE0
glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &texid);
glBindTexture(GL_TEXTURE_2D, texid);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
assert(glGetError() == 0);
}
#ifdef _X_WINDOW_SYSTEM_
#include <X11/Xlib.h>
static EGLNativeWindowType CreateNativeWindow(void)
{
assert((display = XOpenDisplay(NULL)) != NULL);
int screen = DefaultScreen(display);
Window root = DefaultRootWindow(display);
Window window = XCreateWindow(display, root, 0, 0, 600, 480, 0,
DefaultDepth(display, screen), InputOutput,
DefaultVisual(display, screen),
0, NULL);
XMapWindow(display, window);
XFlush(display);
return window;
}
#endif
int display_init(int width, int height)
{
#ifdef _X_WINDOW_SYSTEM_
render_target_init(CreateNativeWindow());
#else
struct mali_native_window window;
window.width = width;
window.height = height;
render_target_init(&window);
#endif
init_GLES(width, height);
}
void render_frame(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0);
//glDrawArrays(GL_TRIANGLES, 0, 3);
eglSwapBuffers(display, surface);
}
void update_texture(void *data, int width, int height)
{
static int first_time = 1;
if (first_time) {
printf("create texture %d %d\n", width, height);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
first_time = 0;
}
else {
printf("update texture %d %d\n", width, height);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, data);
}
assert(glGetError() == 0);
}
int main(void)
{
int i;
void *texture[2] = {0};
int fd = open("0.rgb", O_RDONLY);
struct stat st;
fstat(fd, &st);
texture[0] = malloc(st.st_size);
read(fd, texture[0], st.st_size);
close(fd);
fd = open("1.rgb", O_RDONLY);
fstat(fd, &st);
texture[1] = malloc(st.st_size);
read(fd, texture[1], st.st_size);
close(fd);
display_init(600, 480);
for (i = 0; i < 200; i++) {
update_texture(texture[i%2], 720, 576);
render_frame();
usleep(20000);
}
return 0;
}
vert.glsl
attribute vec3 positionIn;
attribute vec2 texcoordIn;
varying vec2 texcoord;
void main()
{
gl_Position = vec4(positionIn, 1);
texcoord = texcoordIn;
}
frag.glsl
precision mediump float;
uniform sampler2D texMap;
varying vec2 texcoord;
void main() {
vec3 color = texture2D(texMap, texcoord).rgb;
gl_FragColor = vec4(color, 1);
}
Did you try to render the frame without the texture ?
Do like gl_FragColor = vec4(1.0,0.0,0.0,1.0); then check it is still black or red.
If red check your texture function.
I think it can be a problem not to call these functions each a frame
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D,c_Texture[TEXTURES_FIRST].texture_id);
glUniform1i(h_Texture[TEXTURES_FIRST],1);