Qml define custom property group [duplicate] - qml

This question already has answers here:
How to create grouped/nested properties?
(3 answers)
Closed 7 years ago.
How can I define custom property groups in qml, equal to the anchors property?
Example usage:
Item {
anchors {
leftMargin: 5
rightMargin: 5
}
}

I'm afraid this is not as simple as you are thinking it might be.
As far as I know, you have two options:
1.- Follow the recommendation you will see in the link provided by #BaCaRoZzo and implement your own object type.
2.- Write a more complex QML type in C++ and use it in your QML file. More or less the idea pointed out by #folibis. Example:
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QtQml>
#include "customitem.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
qmlRegisterType<CustomItem>("CustomItem", 1,0, "CustomItem");
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
customitem.h
#ifndef CUSTOMITEM_H
#define CUSTOMITEM_H
#include <QObject>
class CustomItem: public QObject
{
Q_OBJECT
/*
* Any property that is writable should have an associated NOTIFY signal.
* Ref: http://doc.qt.io/qt-5/qtqml-cppintegration-exposecppattributes.html#exposing-properties
*/
Q_PROPERTY(int x READ x WRITE setX NOTIFY xChanged)
Q_PROPERTY(int y READ y WRITE setY NOTIFY yChanged)
public:
CustomItem(QObject *parent = 0);
int x() const;
void setX(int);
int y() const;
void setY(int);
private:
int m_x;
int m_y;
signals:
void xChanged();
void yChanged();
public slots:
};
#endif // CUSTOMITEM_H
customitem.cpp
#include "customitem.h"
CustomItem::CustomItem(QObject *parent)
: QObject(parent), m_x(0), m_y(0)
{
}
int CustomItem::x() const
{
return m_x;
}
void CustomItem::setX(int x)
{
m_x = x;
}
int CustomItem::y() const
{
return m_y;
}
void CustomItem::setY(int y)
{
m_y = y;
}
main.qml
import QtQuick 2.5
import QtQuick.Window 2.2
import CustomItem 1.0
Window {
visible: true
CustomItem {
id: customItem
x: 50
y: 50
}
Rectangle {
id: rect
x: customItem.x
y: customItem.y
width: 100; height: 100
color: "red"
}
}

Related

Function not working when inside if or switch statements

I need to solve a OOP problem in which I have to manage multiple classes inherited by each other. First I need to read all the data for all the Employees of a Company. The reading runs very well but I also need to display the read data after reading the command 1 (I need to use switch). I created a function "afisare_angajati()" which only works outside "if" and "switch" statements. I don't know why those statements disable my function. This happened to me before but I couldn't find the cause. Is something that I am not seeing? You can see my function at the end of the code. Thx for help.
#include<iostream>
#include<string>
#include<vector>
class Angajat{
protected:
std::string nume;
float salariu_baza;
std::string functie;
float procent_taxe_salariu;
public:
float get_salariu_net(){return 0;}
float get_salariu_brut(){return 0;}
std::string get_nume(){return 0;}
void marire_salariu(){}
Angajat(std::string nume,float salariu_baza,std::string functie,float procent_taxe_salariu=40):
nume(nume),salariu_baza(salariu_baza),functie(functie),procent_taxe_salariu(procent_taxe_salariu){}
void display(){
std::cout<<nume<<'\n';
std::cout<<functie<<'\n';
}
};
class Analist:public Angajat{
public:
Analist(std::string nume,float salariu_baza,std::string functie,float procent_taxe_salariu=40):
Angajat(nume,salariu_baza,functie,procent_taxe_salariu){}
};
class Programator:public Analist{
protected:
float procent_deducere_salariu_it;
public:
Programator(std::string nume,float salariu_baza,std::string functie,float procent_taxe_salariu=40):
Analist(nume,salariu_baza,functie,procent_taxe_salariu){}
};
class LiderEchipaProgramare:public Programator{
protected:
int vechime;
float bonus_vechime;
public:
LiderEchipaProgramare(std::string nume,float salariu_baza,std::string functie,int vechime,float procent_taxe_salariu=40):
Programator(nume,salariu_baza,functie,procent_taxe_salariu),vechime(vechime){
bonus_vechime=500;
}
};
class FirmaProgramare{
private:
std::vector<Angajat*> vec_ang;
public:
void afisare_angajati(){
for(Angajat* a:vec_ang){
a->display();
}
}
void mareste_salarii(float,float,float){}
void promoveaza(std::string){}
void adauga_angajat(Angajat* a){
vec_ang.push_back(a);
}
};
int main(){
std::string nume;
std::string functie;
float salariu_baza;
int vechime;
int nr_ang,comanda;
FirmaProgramare pula;
std::cin>>nr_ang;
for(int i=0;i<nr_ang;++i){
std::cin.ignore();
std::getline(std::cin,nume);
std::cin>>functie;
std::cin>>salariu_baza;
Angajat* p = nullptr;
if(functie=="Analist"){
p = new Analist(nume,salariu_baza,functie);
}
else{
if(functie=="Programator"){
p = new Programator(nume,salariu_baza,functie);
}
else{
p = new LiderEchipaProgramare(nume,salariu_baza,functie,vechime);
}
}
pula.adauga_angajat(p);
}
std::cin>>comanda;
//pula.afisare_angajati(); output is correct if I put the function outside of brackets
switch(comanda)
{
case 1:{
pula.afisare_angajati();
break;
}
}
}

SystemC - How can I get the module name to which sc_signal connected?

I'd like to print out the name of sc_module to which sc_signal connected.
How can I get the module name, "module_a" in the following code, from "sig_out"?
#include "systemc.h"
class sig_if : virtual public sc_interface
{
};
class my_sig : public sc_module, public sig_if
{
public:
my_sig(sc_module_name nm) : sc_module(nm)
{
}
};
SC_MODULE(test_module)
{
sc_port<sig_if> out;
SC_CTOR(test_module)
{
}
};
int sc_main(int argc, char* argv[]) {
test_module module_a("module_a");
my_sig sig_out("sig_out");
module_a.out(sig_out);
// std::cout << sig_out.get_parent() << std::endl;
sc_start();
return 0;
}
You could override sc_interface::register_port() in the my_sig class to save a reference to the bound port. The parent of the bound port is the module that contains the port.
#include <iostream>
#include <cassert>
#include "systemc.h"
class sig_if : virtual public sc_interface
{
};
class my_sig : public sc_module, public sig_if
{
public:
my_sig(sc_module_name nm) : sc_module(nm), bound_port(NULL)
{
}
void register_port(sc_port_base& port, const char*) {
bound_port = &port;
}
sc_object* get_bound_module() const {
assert(bound_port);
return bound_port->get_parent_object();
};
sc_port_base* bound_port;
};
SC_MODULE(test_module)
{
sc_port<sig_if> out;
SC_CTOR(test_module)
{
}
};
int sc_main(int argc, char* argv[]) {
test_module module_a("module_a");
my_sig sig_out("sig_out");
module_a.out(sig_out);
sc_start(0, SC_NS);
std::cout << sig_out.get_bound_module()->name() << std::endl;
return 0;
}
The above code prints the name of module_a. Note that sig::get_bound_module() can only be called after elaboration, that is, after sc_start() has been called, because sc_interface::register_port() is only called during elaboration.

How do I setup Using Systems::IO::Ports in DLL so that it can be called from a loadlibrary function

Using vc2012 express c++
I am a little confused on how a runtime library works, but I had needed to create one for a driver from some hardware I have so that it can be used in a SDK.
My source code is as follows
#include "PhantomAdapter.h"
#include <stdexcept>
int ready()
{
//return Comms::SerialPort::check();
return 1;
}
int open()
{
int flag=0;
//flag=Comms::SerialPort::openPort();
return flag;
}
int close()
{
Comms::SerialPort::closePort();
return 1;
}
int angle(double& angle)
{
angle = Comms::SerialPort::read();
return 0;
}
int torque(double torque)
{
Comms::SerialPort::send((Byte)torque);
return 1;
}
namespace Comms
{
//static p1 = gcnew System::IO::Ports::SerialPort();
int SerialPort::openPort()
{
bool check=0;
p1 = gcnew System::IO::Ports::SerialPort();
p1->BaudRate = 57600;
p1->PortName = "COM3";
if(p1->IsOpen)
return 0;
else {
p1->Open();
return 1;
}
}
int SerialPort::check()
{
array<String^>^ serialPorts = nullptr;
bool flag = true;
serialPorts = p1->GetPortNames();
for each(String^ port in serialPorts)
{
if(port=="COM3")
flag= true;
}
return flag;
}
void SerialPort::closePort()
{
p1->Close();
}
void SerialPort::send(Byte data)
{
array<unsigned char>^ buffer = gcnew array<Byte>(1);
buffer[0] = (char)data;
p1->Write(buffer,0,1);
}
double SerialPort::read()
{
double data;
data = p1->ReadByte();
return data;
}
}
header
#define PHANTOMADAPTER_API __declspec(dllexport)
#else
#define PHANTOMADAPTER_API __declspec(dllexport)
#endif
#using <mscorlib.dll>
#using <system.dll>
using namespace System;
using namespace System::IO::Ports;
using namespace System::Threading;
extern "C" {
PHANTOMADAPTER_API int ready();
PHANTOMADAPTER_API int open();
PHANTOMADAPTER_API int close();
PHANTOMADAPTER_API int angle(double& angle);
PHANTOMADAPTER_API int torque(double torque);
}
namespace Comms
{
public ref class SerialPort
{
private:
static System::IO::Ports::SerialPort^ p1;
public:
static int openPort();
static void closePort();
static double read();
static void send(Byte data);
static int check();
};
}
I am getting the following error when I call the angle DLL function or any function that requires the Comms namespace.
System.NullReferenceException: Object reference not set to an instance of an object.
at System.IO.Ports.SerialPort.get_IsOpen()
at System.IO.Ports.SerialPort.ReadByte()
at angle(Double* angle)
can someone please point me in the right direction, I feel as if the serialPort class can't be open from runtime library unless I import it somehow

Changing QIcon size in QStandardItemModel

I am trying to make QTableView/QStandardItemModel with arbitrary sized qIcons. In the MWE below, I have successfully changed the height of a row using a delegate. I can't figure out how to make it use a larger icon size in the larger row. Any help appreciated. Note that all rows can be the same height as long as I can set that height. For example, how do I make the icons 50x50 in the example below.
#include <QtGui>
#include "test.h"
//////////////////////////////////////////////////////////////////////////
// TEST.H file:
//#include <QtGui>
//
//class MainWindow : public QMainWindow {
// Q_OBJECT
//
// public:
// MainWindow(QWidget *parent = 0);
//};
class ItemDelegate : public QItemDelegate {
public:
ItemDelegate() {}
QSize sizeHint ( const QStyleOptionViewItem &, const QModelIndex & ) const {
return QSize(50,50);
}
};
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
QImage iconImage(100, 100, QImage::Format_RGB32);
iconImage.fill(0xF08080);
const int ARRAY_HEIGHT = 2;
const int ARRAY_WIDTH = 2;
QStandardItemModel *model = new QStandardItemModel( ARRAY_HEIGHT, ARRAY_WIDTH );
model->setHeaderData( 1, Qt::Horizontal, QString("Icon") );
model->setHeaderData( 2, Qt::Horizontal, QString("Text") );
for (int yy=0; yy < ARRAY_HEIGHT; yy++ ) {
QStandardItem *newIconItem = new QStandardItem;
newIconItem->setIcon( QPixmap::fromImage( iconImage ) );
model->setItem( yy, 0, newIconItem );
QStandardItem *newTypeItem = new QStandardItem( QString("Foo") );
model->setItem( yy, 1, newTypeItem );
}
QTableView *table = new QTableView;
ItemDelegate *delegate = new ItemDelegate();
table->setModel(model);
table->setItemDelegate(delegate);
table->setSelectionMode(QAbstractItemView::SingleSelection);
table->setSelectionBehavior(QAbstractItemView::SelectRows);
table->verticalHeader()->setVisible(false);
table->verticalScrollBar()->setVisible(false);
table->resizeRowsToContents();
table->resizeColumnsToContents();
QHeaderView *headerView = table->horizontalHeader();
headerView->setStretchLastSection(true);
setCentralWidget( table );
}
int main( int argc, char *argv[] ) {
QApplication app(argc, argv);
MainWindow win;
win.show();
return app.exec();
}
try
ListView->setIconSize(QSize(100,100));

How to be notified on focus?

This question has been edited:
I am working with QML.
I have a custom type called polygon, which is a subcalss of QDeclarativeItem.
I want to be notified when a mouse clicked on a polygon(has focus).
I know QDeclarativeItem has a function: focusInEvent.
I override it in Polygon.cpp, here is polygon.cpp
#include "polygon.h"
#include "point.h"
#include <QPainter>
#include <stdio.h>
#include <iostream>
#include <sstream>
#include <QGraphicsSceneDragDropEvent>
#include <QFocusEvent>
#include "DeclarativeDragDropEvent.h"
using namespace std;
using namespace Qt;
Polygon::Polygon(QDeclarativeItem *parent)
: QDeclarativeItem(parent)
{
// need to disable this flag to draw inside a QDeclarativeItem
setFlag(QDeclarativeItem::ItemHasNoContents, false);
setFlags(ItemIsSelectable|ItemIsMovable|ItemIsFocusable);
setAcceptDrops(true);
}
QVariant Polygon::itemChange(GraphicsItemChange change, const QVariant &value)
{
return QGraphicsItem::itemChange(change, value);
}
void Polygon::focusInEvent ( QFocusEvent * event ){
cout<<"focusin"<<endl;
}
QRectF Polygon::boundingRect() const{
QVector<QPointF> vPnt=listToVector(m_vertices);
return QPolygonF(vPnt).boundingRect();
}
QPainterPath Polygon::shape () const
{
QPainterPath path;
QVector<QPointF> vPnt=listToVector(m_vertices);
path.addPolygon(QPolygonF(vPnt));
return path;
}
QString Polygon::name() const
{
return m_name;
}
void Polygon::setName(const QString &name)
{
m_name = name;
}
QColor Polygon::color() const
{
return m_color;
}
void Polygon::setColor(const QColor &color)
{
m_color = color;
}
void Polygon::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
QPen pen(m_color, 2);
painter->setPen(pen);
painter->setRenderHints(QPainter::Antialiasing, true);
QVector<QPointF> vPnt=listToVector(m_vertices);
painter->setBrush(QBrush(m_color,Qt::SolidPattern));
painter-> drawPolygon(QPolygonF(vPnt),Qt::OddEvenFill);
}
QVector<QPointF> Polygon:: listToVector(QList<Point *> lpnt) const{
QVector<QPointF> vPnt;
for(int i=0;i<lpnt.length();i++){
vPnt.append(QPointF(lpnt.at(i)->x(),lpnt.at(i)->y()));
}
return vPnt;
}
QDeclarativeListProperty<Point> Polygon::vertices()
{
return QDeclarativeListProperty<Point>(this, 0, &Polygon::append_vertex);
}
void Polygon::append_vertex(QDeclarativeListProperty<Point> *list, Point *vertex)
{
Polygon *polygon = qobject_cast<Polygon *>(list->object);
if (polygon) {
vertex->setParentItem(polygon);
polygon->m_vertices.append(vertex);
}
}
And here is my qml file:
import MyTypes 1.0
import QtQuick 1.0
import Qt 4.7
Item {
id:container
width: 300; height: 200
Polygon {
id: aPolygon
anchors.centerIn: parent
width: 100; height: 100
name: "A simple polygon"
color: "blue"
vertices:[
Point{x:20.0; y:40.0},
Point{x:40.0; y:40.0},
Point{x:40.0; y:20.0},
Point{x:20.0; y:20.0}
]
}
}
i override shape, boundingRect as you can see to define Polygon object and to succeed to be notified when a click, a focus appeared in ti
But i can not see cout output in focusInEvent function on screen.
Should i add something on main.cpp of project?
What to do to C++ code know that the object has focus?
Some connection?
Thanks for any idea.
I tried and it seems the problem is with the output "cout", try the "qDebug" and see that you have good code.
All you did is correct.