Designing simple integrated circuits simulator
-
Hi,i am currently a student and i've been given a certain assignment.I need to create a program which should draw some basic circuits and also calculate the output given the certain input.I have already created the basic GUI and some other stuff,but i have a problem.I have no idea how to connect different circuits with a line,for example if we have one "and" and one "nor" circuit and we wanted to connect them with a line.I don't know how to program that..Any ideas,based on my code so far?If anyone has any ideas,i would be extremely grateful.Believe it or not,i've only been given a couple of weeks for this project,it's not easy,and it's all in the middle of the exam period!!Thats how bad education system is in my country.The variable names are mostly in serbian,but understanding what they do should be straight-forward without the names being in english.Also,if possible,it would be good to be able to move the connecting line when i move any of the circuits..Of course,i want to be able to connect the circuits with other circuits when i press some button from my buttongroup
This is the link for my full code(circuit.rar file):http://www.mediafire.com/?vvbcjok02m3avwu
Thanks again for any help
Nikolas -
We have had a similar discussion on this forum before. Did you search the archives?
-
i have found some examples,but they are not suitable for how i implemented this,,,i know i need to define a new class,called lets say link which will connect them,but the problem is that i need to be able to connect different circuits,not only one type of circuit;i was thinking of implementing a new class called circuit which will be inherited by all circuits,so that the link can be created between two items of type circuit..but the problem is how will i connect all that to my button group,i'm bad when it comes to event handling..besides i've only been studying Qt for barely more than a week...anyway,,,something simpler,,,any idea how to implement drawing a line between 2 different mouse clicks??for instance,you press(click on) some button,then you click once anywhere in the scene,then you click the second time and then the line is drawn between those 2 points which you clicked with a mouse???this is a way simpler solution,but the real problem will occur when i need to calculate the output.
thanks -
Note that the fact that you're under time pressure isn't our problem. "It is yours.":http://www.catb.org/~esr/faqs/smart-questions.html#urgent Sorry about that.
There are, of course, many ways that you could go about building what you have to do. My advice would be to use QGraphicsView and related classes. Make each item in your diagram a class that inherits (directly or indirectly) from QGraphicsItem, and connect the items by lines (QGraphicsLineItem). So, you would not be drawing a line between two mouse clicks. Instead, the mouse clicks would identify two object in your QGraphicsScene, that you then connect using a new QGraphicsLineItem. The items on your QGraphicScene would together represent your complete circuit.
-
i have a following question:if we had two different items which are inherited from QGraphicsItem,how could we connect them,with a line..also,lets say we moved those items,how could we move the line too?any ideas,a concrete code example would be highly appreciated ..thanks
Nickcode example:
@#ifndef ANDITEM_H
#define ANDITEM_H
#include <QGraphicsItem>
#include "scena.h"class andItem : public QGraphicsItem
{
public:
andItem();QRectF boundingRect() const; QPainterPath shape() const; void paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget);
};
#endif // ANDITEM_H
//////
#include <QtGui>
#include "anditem.h"
#include <QGraphicsScene>
#include <QPainter>
#include <QStyleOption>andItem::andItem()
{
setFlag(QGraphicsItem::ItemIsMovable, true); setFlag(QGraphicsItem::ItemIsSelectable, true);
}
QRectF andItem::boundingRect() const{
const QPointF *p0 = new QPointF(0,0);
const QPointF *p1 = new QPointF(100,100);
return QRectF(*p0,*p1);
}void andItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){ Q_UNUSED(option); Q_UNUSED(widget); if(painter == 0) painter = new QPainter(); painter->setRenderHint(QPainter::Antialiasing); painter->scale(0.4,0.4); painter->drawLine(0,50,50,50); painter->drawLine(0,100,50,100); painter->drawLine(50,25,50,125); painter->drawLine(50,25,100,25); painter->drawLine(50,125,100,125); painter->drawArc(QRect(62.5,25, 75,100), -90*16, 180*16); painter->drawLine(137.5,75,200,75); } QPainterPath andItem::shape() const { QPainterPath path; path.addRect(boundingRect()); return path; }
/////////////////and another item now
#ifndef XORITEM_H
#define XORITEM_H
#include <QGraphicsItem>class xorItem : public QGraphicsItem
{
public:
xorItem();QRectF boundingRect() const; QPainterPath shape() const; void paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget);
};
#endif // XORITEM_H//////////////
#include <QtGui>
#include <QGraphicsScene>
#include <QPainter>
#include <QStyleOption>xorItem::xorItem()
{setFlag(QGraphicsItem::ItemIsMovable, true); setFlag(QGraphicsItem::ItemIsSelectable, true);
}
QRectF xorItem::boundingRect() const{
const QPointF *p0 = new QPointF(0,0);
const QPointF *p1 = new QPointF(100,100);
return QRectF(*p0,*p1);
}void xorItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){ QPainterPath path1; Q_UNUSED(option); Q_UNUSED(widget); if(painter == 0) painter = new QPainter(); painter->setRenderHint(QPainter::Antialiasing); painter->scale(0.4,0.4); painter->drawLine(0,50,50,50); painter->drawLine(0,100,50,100); painter->drawArc(QRect(5,25, 50,100), -80*16, 160*16); painter->drawArc(QRect(-5,25, 40,100), -80*16, 160*16); painter->drawLine(35,25,100,25); painter->drawLine(35,125,100,125); path1.moveTo(100, 25); path1.cubicTo(130, 25, 160, 50, 170, 75); path1.moveTo(100, 125); path1.cubicTo(130, 125, 160, 100, 170, 75); painter->drawPath(path1); painter->drawLine(170,75,232.5,75); } QPainterPath xorItem::shape() const { QPainterPath path; path.addRect(boundingRect()); return path; }
@
-
Create a LineItem class with references to the two items to be connected. The LineItem draws the line between the two items. When the items move, the LineItem must be notified so that it can update.
-
i have an item which inherits from GraphicsItem and i have another Item which inherits from GraphicsLineItem.I am using the second item to connect any two items which belong to the first class.In my code i am able to connect any two of the items with one line,but i want to be able to connect first item with the second one,not only with one line,but with two lines,and i want each of those two lines to connect to the second item at different places,i don't want the lines to overlap.So,lets say,we had two items and we wanted to connect them.The first line goes from lets say the middle of the first one and connects to the second item at some point p1.Then i want to create another line from the first item to the second one,also from the middle of the first item to some other point of the second one,lets say p2.What could i add to the following code to achieve that.Any suggestions?I have ideas,but i can't seem to implement them.Thanks very much
Niko"This is my code":http://alas.matf.bg.ac.rs/~mi07137/code.txt
I couldn't post it here because it's too large
///////////////after this,i create the mainwindow,put two buttons there and connect each-one with a certain type,the one connected to the type "And" will draw graphicsItems i defined in and.cpp file here and the one connected to the type "Povezi" will draw lines connecting every two items...
Based on this code I can draw more than one line that connects any two items,but i dont want them to overlap,i want them to be distinct,aka end at different points of another item.I know the key is in the updatePosition() function defined in Linija.cpp file..P.S.This example is based on DiagramScene example which is nicely explained in GraphicsView/Qt examples