在 pixMapItem 上覆盖鼠标事件对我不起作用;pixMapItem 未检测到鼠标单击事件。这是我的代码:
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class DrawImage( QMainWindow ):
def __init__(self, path):
QMainWindow.__init__(self)
self.setWindowTitle('Select Window')
self.local_image = QImage(path)
self.local_grview = QGraphicsView()
self.setCentralWidget( self.local_grview )
self.local_scene = QGraphicsScene()
self.image_format = self.local_image.format()
self.pixMapItem = self.local_scene.addPixmap( QPixmap(self.local_image) )
self.local_grview.setScene( self.local_scene )
self.pixMapItem.mousePressEvent = self.pixelSelect
self.show()
sys.exit(app.exec_())
def pixelSelect( self, event ):
print 'hello'
position = QPoint( event.pos().x(), event.pos().y())
color = QColor.fromRgb(self.local_image.pixel( position ) )
if color.isValid():
rgbColor = '('+str(color.red())+','+str(color.green())+','+str(color.blue())+','+str(color.alpha())+')'
self.setWindowTitle( 'Pixel position = (' …Run Code Online (Sandbox Code Playgroud) 我一直在尝试验证正确的坐标格式.目前我想保存坐标.但是当格式错误时,标记将不会出现在我的地图上.
这是我的一些示例代码.
String latlon = "3.169054, 101.714108";
String regex_coords = "/\\((\\-?\\d+\\.\\d+), (\\-?\\d+\\.\\d+)\\)/";
Pattern compiledPattern2 = Pattern.compile(regex_coords, Pattern.CASE_INSENSITIVE);
Matcher matcher2 = compiledPattern2.matcher(latlon);
while (matcher2.find()) {
System.out.println("Is Valid Map Coordinate: " + matcher2.group());
}
Run Code Online (Sandbox Code Playgroud)
我希望用户能够使用逗号将此格式仅保存为"3.169054,101.714108".
任何人都可以帮助我构建正则表达式以有效这种格式吗?
谢谢大家
我有一个具有 (x,y) 坐标的事件列表,其中 x 的范围从 1 到 100,y 的范围从 1 到 86。每个坐标都有(通常有很多)重复项。我想用每个坐标的计数填充一个矩阵(实际上是一个数字网格)。我该怎么做呢?
现在,我最好的尝试是:
s=matrix(data=NA,nrow=n,ncol=k)
for(i in 1:n){
for(j in 1:k){
s[i,j]=nrow(subset(data,x_column==i & y_column==j))
}
}
Run Code Online (Sandbox Code Playgroud)
这适用于小型(约 10,000 行)数据帧,但我想针对近 300 万行的数据帧运行它,而我的方法太慢了。
编辑(数据):
n=86;k=100;
x_column y_column
54 30
51 32
65 34
19 46
51 27
45 60
62 31
64 45
16 69
31 33
Run Code Online (Sandbox Code Playgroud)
多谢你们!
编辑:好吧,事实证明该程序的速度足以满足我的需求——我的工作区陷入了大量数据的泥潭,它减慢了我尝试做的所有事情。所以我的方法是有效的,但了解填充矩阵的替代方法是有好处的。我上传了前 10 行;有人可以做一下速度测试吗?
我从谷歌地图得到一个坐标,我想将它分配给一个浮点变量,但它显示了这个错误:
无法指定"CLLocationDegrees"类型的值(也称为"Double")来键入"Float!"
func markerButtonClicked(sender:MapButton) {
let coord = self.getCenterCoordinate()
let addressObj = Address()
addressObj.latitude = coord.latitude
addressObj.longitude = coord.longitude
}
func getCenterCoordinate() -> (CLLocationCoordinate2D) {
let location = CGPoint(x:self.mView.bounds.size.width/2,y:self.mView.bounds.size.height/2)
let coord = self.mView.projection .coordinate(for: location)
return coord
}
class Address: NSObject {
var latitude:Float!
var longitude:Float!
}
Run Code Online (Sandbox Code Playgroud) 我有一个班级Room和一个班级World.目前,我有一个
Dictionary<Point, Room> world;
Run Code Online (Sandbox Code Playgroud)
我存储Room的是这样的:
world.Add(new Point(0,0), new Room());
Run Code Online (Sandbox Code Playgroud)
但是当我尝试访问它时,它返回null:
world.Get(new Point(0,0));
Run Code Online (Sandbox Code Playgroud)
我理解为什么会这样.但我的问题是:有人知道更好的方法吗?
我试图围绕协调异步操作.我需要一点帮助理解:
大多数情况下,向$ .get()这样的异步函数添加回调效果很好.然而,在某种程度上,这确实决定了我的代码编写的方式和位置,我不喜欢.
$(function() {
var r = AjaxResults( page );
// loop prevents further processing until GetResults() is done
while( isComplete == false )
isComplete = $('body').data('isComplete');
// reset isComplete boolean
$('body').data('isComplete', false);
// this will only continue after GetResults() has exited
paintTheTownRed();
var whateverElse = true;
});
function AjaxResults( page ) {
// do something before async operation
switch( page ){
case '1': {...} break;
...
default: break;
}
$.getJSON("/controller/page", null, function(data) {
// do something …Run Code Online (Sandbox Code Playgroud) 我在使用我创建的"画布"类中遇到了一个问题,这是一个扩展的类JPanel,用于绘制动画环形图.此图表使用a MouseListener来获取点击事件.
问题是鼠标位置似乎不准确,这意味着它似乎不是相对于"画布"而是相对于窗口(在左上角,我得到约30px的y coord).
这是我的代码:
我创建了一个类,它扩展了JPanel并且有一个BufferedImage作为成员.
public class Canvas extends JPanel {
public BufferedImage buf;
private RingChart _parent;
public Canvas(int width, int height, RingChart parent){
buf = new BufferedImage(width, height, 1);
...
Run Code Online (Sandbox Code Playgroud)
在paint组件方法中,我只绘制缓冲图像,因此我可以通过在缓冲图像上绘制来从"外部"绘制画布,这是公共的.
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.drawImage(buf, null, 0, 0);
}
Run Code Online (Sandbox Code Playgroud)
现在有一个类RingChart包含一个"画布":
public class RingChart extends JFrame{
public Canvas c;
...
Run Code Online (Sandbox Code Playgroud)
我从canvas类的bufferedImage创建了一个Graphics2D.这个g2d用于绘画:
public RingChart(){
c = new Canvas(1500,980,this);
add(c);
setSize(1500, 1000);
setTitle("Hans");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
g2d = (Graphics2D)c.buf.createGraphics();
...
Run Code Online (Sandbox Code Playgroud)
我现在想要实现的是一个鼠标监听器,它监听画布上发生的鼠标事件.因此,当用户点击画布时,我可以通过事件变量检索他在画布上单击的位置.
所以我创建了一个鼠标监听器:
class …Run Code Online (Sandbox Code Playgroud) 我有一些兴趣点,并希望对它们进行硬编码,但似乎没有一个干净的设施在objective-c中这样做.
我想将两个数字关联到一个字符串:@"cool spot",37.77794,-122.41933
我是否需要NSStrings的NSDictionary并将每个浮点数转换为字符串?将所有这些分组似乎太多了?我应该制作一个c-struct吗?这种情况有很多,但无法想到这可能是一个干净的解决方案.
谢谢
我想在C++中用VBO创建网格类.该课程如下:
mesh::mesh(std::vector<Vector3d>* Vertices, std::vector<unsigned int>* Indices, std::vector<Vector2d>* TextureCoords)
{
if(Vertices) this->vertices = *Vertices;
if(Indices) this->indices = *Indices;
if(TextureCoords) this->textureCoords = *TextureCoords;
chatbox.AddMessageToQueue("vertices.size() : %d", vertices.size());
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, vertices.size()*sizeof(Vector3d) + textureCoords.size()*sizeof(Vector2d), 0, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, vertices.size()*sizeof(Vector3d), vertices.data());
glBufferSubData(GL_ARRAY_BUFFER, vertices.size()*sizeof(Vector3d), textureCoords.size()*sizeof(Vector2d), textureCoords.data());
glGenBuffers(1, &IND);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IND);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size()*sizeof(unsigned int), &indices[0], GL_STATIC_DRAW);
}
mesh::~mesh()
{
glDeleteBuffers(1, &VBO);
glDeleteBuffers(1, &IND);
}
void mesh::draw()
{
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glVertexPointer(3, GL_FLOAT, 0, (void*)0);
glTexCoordPointer(2, GL_FLOAT, 0, (void*)(sizeof(float)*3*6));
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IND);
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, …Run Code Online (Sandbox Code Playgroud) 我有一个名为"buttonPressed"的按钮.当我按下按钮时,我需要找到该按钮的x和y坐标.你有什么想法可以帮助我吗?
UIButton *circleButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
circleButton.frame = rect;
circleButton.layer.cornerRadius = 8.0f;
[circleButton addTarget:self
action:@selector(buttonPressed:)
forControlEvents:UIControlEventTouchUpInside];
[self addSubview:circleButton];
-(void)buttonPressed:(id)sender
{
}
Run Code Online (Sandbox Code Playgroud)
回答
- (IBAction)buttonPressed:(UIButton *)sender
{
CGPoint coordinates = sender.frame.origin;
CGFloat x = coordinates.x;
CGFloat y = coordinates.y;
}
Run Code Online (Sandbox Code Playgroud)