小编Yel*_*low的帖子

来自QPainter的文本比QPainterPath好得多

我想使用绘制文本QPainter,我想先使用QPainterPath(因为最终我想以各种方式旋转文本).但是,我发现生成的文本QPainterPath比生成的文本更加丑陋QPainter.

以下代码:

void MyWidget::paintEvent(QPaintEvent* /*event*/) {

     QFont font;
     font.setStyleHint(QFont::Times, QFont::PreferAntialias);
     font.setPointSize(30);

     QPainter painter;
     painter.begin(this);
     painter.setRenderHint(QPainter::Antialiasing);
     painter.setBrush(Qt::black);
     painter.setFont(font);
     painter.drawText(10, 40, "Hello World");

     QPainterPath textPath;
     textPath.addText(10, 100, font, "Hello world");
     painter.drawPath(textPath);

     painter.end();
}
Run Code Online (Sandbox Code Playgroud)

产生以下结果:

在此输入图像描述

前者显然更清洁,更好,特别是在较小的字体中.我该怎么做才能得到相同的结果QPainterPath?

我在Qt 5.0的Windows 7计算机上生成上述结果.

c++ qt qpainter qt5

7
推荐指数
2
解决办法
3511
查看次数

在DataGridViewTextBoxColumn中以数字方式排序

这个问题与这两个问题密切相关(这个和这个),但我不认为他们给出了令人满意的答案.

我有一个DataGridView(即一个表)有几个DataGridViewTextBoxColumn不同数据类型的列(),字符串,整数和浮点数.当我单击它们各自的标题时,每个标题应根据其类型进行排序:按字母顺序排列字符串,按数字排序.我简单地说,我有以下代码:

private System.Windows.Forms.DataGridView grid;
private System.Windows.Forms.DataGridViewTextBoxColumn stringColumn;
private System.Windows.Forms.DataGridViewTextBoxColumn doubleColumn;
private System.Windows.Forms.DataGridViewTextBoxColumn intColumn;


stringColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
doubleColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
intColumn    = new System.Windows.Forms.DataGridViewTextBoxColumn();

grid.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
        stringColumn,
        doubleColumn,
        intColumn});
Run Code Online (Sandbox Code Playgroud)

但是,由于默认表示是string,数值也会按字母顺序排序,例如:

1, 11, 2, 3, 42, 5
Run Code Online (Sandbox Code Playgroud)

显然,作为一种简单的解决方法,根据一些线程(例如这里和这里),以下应该立即解决问题:

doubleColumn.ValueType = typeof(double);
intColumn.ValueType = typeof(int);
Run Code Online (Sandbox Code Playgroud)

但是,此解决方案在我的项目中不起作用:值仍按字母顺序排序.所以问题是:为什么不呢?在调试器中,我可以看到值类型实际上已更改为(在这种double情况下)System.Double,所以至少发生了一些事情.但是,如何在不编写自己的分拣机的情况下确保相应地对其进行排序?

c# sorting datagridview winforms

7
推荐指数
2
解决办法
1万
查看次数

如何在窗口标题中设置StaticResource绑定

我想对IValueConverter窗口的标题进行绑定,以便在活动项目更改时进行更改.问题是值转换器是一个静态资源,以后只加载几行:

<Window x:Class="MyProject.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:MyProject"
        Height="600" Width="800" VerticalAlignment="Stretch"
        Title="{Binding ActiveProject, Converter={StaticResource windowTitleConverter}},  UpdateSourceTrigger=PropertyChanged">
     <Window.Resources>
         <local:MainWindowTitleConverter x:Key="windowTitleConverter"/>
     </Window.Resources>

     <!-- Rest of the design -->
</Window>
Run Code Online (Sandbox Code Playgroud)

然后是转换器的定义:

public class MainWindowTitleConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == null) return "Programme"; else return "Programme: " + (value as string);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
Run Code Online (Sandbox Code Playgroud)

这崩溃了,大概是因为StaticResource尚未加载(我想不出任何其他原因),因为没有转换器它工作正常.但是,我无法改变顺序.我试图把它放在一个<Window.Title> …

wpf binding staticresource

6
推荐指数
1
解决办法
4002
查看次数

在Qt中是否有办法禁止计算机进入睡眠状态?

我正在编写一个Qt应用程序来执行一些繁重的计算,但是在Mac上(可能也在Windows上但我目前无法检查这个),一旦计算机进入休眠模式,执行就会停止.

即使屏幕转到屏幕保护程序或空白,我想要一种继续执行的方法.显然,你可以要求用户改变他/她的能量设置,但这远非理想的解决方案.有没有正确的方法呢?

c++ macos qt qt5

6
推荐指数
2
解决办法
2245
查看次数

如何使用模板绑定继承控件的所有属性

我正在尝试制作我自己的控件版本,比如 a TextBox,我想向其中添加一些自定义 UI。我想从这个类继承,这样用户仍然可以像调用普通文本框一样调用我的特殊文本框。我有以下几点:

// MySpecialTextBox.cs

public class MySpecialTextBox : TextBox
{
    static MySpecialTextBox () { }   

    /* Some special properties */     
}
Run Code Online (Sandbox Code Playgroud)

在 XAML 中:

<Style TargetType="{x:Type MySpecialTextBox}">        
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type MySpecialTextBox}">
                <StackPanel>
                    <Rectangle Width="100" Height="3" Fill="Pink" /> <!-- My own fancy lay-out -->
                    <TextBox Text="{TemplateBinding Text}"/>         <!-- This text box should 'inherit' ALL the properties from the caller -->
                </StackPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)

我的问题是,现在,TextBoxonly 获取您在此模板中明确设置的属性,在这种情况下,只有Text. 我想也结合Background,Foreground以及所有其他可能的属性。显然我可以做这样的事情: …

wpf xaml datatemplate

6
推荐指数
1
解决办法
1509
查看次数

glGenBuffers在Release Build中崩溃

我在使用OpenGL函数glGenBuffers()时面临一个奇怪的问题.我正在编写一个相当简单的应用程序,其中我使用以下列方式声明的VBO:

#include <QGLFunctions>
#include <QGLWidget>

class MyClass : public QGLWidget, protected QGLFunctions {
    GLuint vertexBufferObject;

    // ...
    GLuint makeBufferList(void);
}

GLuint MyClass::makeBufferList(void) {
    vertexBufferObject = 0;
    glGenBuffers(1, &vertexBufferObject);  // <-- Here it crashes

    // ... load data and render

    return vertexBufferList;
}

MyClass::MyClass(QWidget* parent) 
    : QGLWidget(parent),
      vertexBufferObject(0)
{
    QGLContext* context = new QGLContext(this->format());
    initializeGLFunctions(context);
    glInit();
}

MyClass::~MyClass() {
    glDeleteBuffers(1, &vertexBufferObject);
}
Run Code Online (Sandbox Code Playgroud)

这一切在Debug Build中完全正常.数据渲染得很好,所有程序最终都能正确完成.但是,在Release Build中,glGenBuffers()会使程序崩溃.它不仅返回0或什么都不做,它在函数调用时崩溃.但由于问题只发生在Release模式下,我无法使用调试器找出出错的地方.

我正在研究Windows 7系统并在Qt 4.8.1中进行开发.编译器是MSVC 2010(Qt SDK)编译器.

有没有人有任何我可能会尝试的建议?

//编辑:

也许有用的知道:我尝试使用GCC(Qt SDK)编译器在Mac上编译完全相同的代码,并且Debug和Release Build都可以正常工作.但在Windows 7上,问题仍然存在.

c++ opengl qt4

5
推荐指数
1
解决办法
2078
查看次数

MediaElement是否仅在嵌入XAML代码时播放?

我有一个声音播放器类,根本没有任何视觉效果,我正在尝试使用a MediaElement播放我的声音.在所有测试项目中,它MediaElement都嵌入在XAML代码中,它工作正常.但是,在我的仅代码版本中,即使文件已经完全加载(我可以在调试器中看到),它根本不会播放任何内容.我正在做以下事情:

public class MySoundPlayer
{
    private MediaElement player = new MediaElement();

    public MySoundPlayer()
    {
        player.LoadedBehavior = MediaState.Manual;
        player.UnloadedBehavior = MediaState.Stop;
        player.Volume = 1.0;
        player.MediaEnded  += player_MediaEnded;
        player.MediaOpened += playerr_MediaOpened;
        player.MediaFailed += player_MediaFailed;
    }

    private void player_MediaEnded(object sender, EventArgs e)
    {
        player.Stop();
        Debug.WriteLine("Stopped");
     }

    private void player_MediaOpened(object sender, EventArgs e)
    {
        Debug.WriteLine("Opened");
    }

    private void player_MediaFailed(object sender, ExceptionRoutedEventArgs e)
    {
        Debug.WriteLine("Failed");
    }

    public void PlayFile(string fileName, bool loop)
    {
        player.Source = new Uri(fileName, UriKind.RelativeOrAbsolute);
        player.Play();
        player.Volume = …
Run Code Online (Sandbox Code Playgroud)

c# audio wpf xaml mediaelement

5
推荐指数
1
解决办法
4147
查看次数

Qt 5.2版本未在Mac上正确安装

我最近尝试将我的Qt版本升级到5.2,因此我卸载了以前的版本(使用MaintenanceTool.app),并下载并安装了最新版本.但这完全打破了它.每当我尝试建立一个项目时,它会说:

The Qt version is invalid: Qt version is not properly installed, please run make install.
Run Code Online (Sandbox Code Playgroud)

在打开示例项目时,它在常规消息中说:

Error while parsing file /Users/thisuser/Qt/5.2.0/5.2.0/clang_64/examples/gui/analogclock/analogclock.pro. Giving up.
Could not find qmake configuration file default.
Cannot read /usr/local/Qt-5.2.0/mkspecs/macx-clang/qmake.conf: No such file or directory
Could not read qmake configuration file /usr/local/Qt-5.2.0/mkspecs/macx-clang/qmake.conf.
Project ERROR: Unknown module(s) in QT: gui core
Project ERROR: Unknown module(s) in QT: gui core
Run Code Online (Sandbox Code Playgroud)

我检查了这个路径(/usr/local/Qt-5.2.0/mkspecs/macx-clang/),确实没有这样的东西:甚至没有/usr/local/Qt-5.2.0目录.我已经再次卸载了所有内容然后再次安装,但这根本没有帮助.有没有人有什么建议?

我的操作系统是OSX Lion.

macos installation qt osx-lion qt5

5
推荐指数
1
解决办法
2689
查看次数

UWP App 总是以非零返回码退出

据我了解,未完成并返回 0 错误代码的应用程序未正确终止。但不知何故,我制作的任何 UWP 应用程序,即使是非常默认的应用程序,每次都以错误代码 1 结束。此外,它的主类Dispose()和析构函数永远不会被调用。这是预期的行为吗?

为了重现,我刚刚制作了默认的 UWP 应用程序:

  • 右键单击解决方案 -> 添加 -> 新建项目...
  • 然后在其他语言 -> Visual C# -> Windows Universal -> "Blank App (Universal Windows)"
  • 然后构建和部署应用程序。

如果您现在运行然后关闭该应用程序,它似乎正在被强制退出。我得到的最后输出是:

The thread 0x35a4 has exited with code 0 (0x0).
The thread 0x34f4 has exited with code 1 (0x1).
The program '[3376] App1.exe' has exited with code 1 (0x1).
Run Code Online (Sandbox Code Playgroud)

所以我认为这意味着一个线程失败,然后应用程序失败。这是正确的,这是坏的吗?

此外,我在 main 中添加了以下代码App.xaml.cs:

    ~App()
    {
        System.Diagnostics.Debug.WriteLine("Calling destructor");
    }

    // (Implement Disposable interface)
    public void Dispose() 
    {
        System.Diagnostics.Debug.WriteLine("Calling …
Run Code Online (Sandbox Code Playgroud)

c# destructor uwp

5
推荐指数
1
解决办法
344
查看次数

如何在DataGridView中使用指定的DataSource启用排序?

在几个建议,我开始分配DataSource给我DataGridView而不是使用DataGridView.Rows.Add(...).这很方便,因为我的数据源已经是一个不会改变的大列表(很多).但是,当我使用DataSource赋值时,无法对列进行排序.

class MyGridView : DataGridView
{
    private List<Person> m_personList;

    private class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public Person(string first, string last)
        {
            FirstName = first;
            LastName = last;
        }
    }

    public MyGridView()
    {
         /* ...initialise stuff... */
         m_personList.Add(new Person("Kate", "Smith"));
         m_personList.Add(new Person("Bill", "Davids"));
         m_personList.Add(new Person("Ann", "Roth"));

         this.DataSource = m_personList; 
    }
}
Run Code Online (Sandbox Code Playgroud)

我也试图取代List<Person>通过BindingList<Person>和通过BindingSource,但没有,这似乎无关紧要.我还尝试添加自定义排序器:

this.SortCompare += MyGridView_SortCompare; …
Run Code Online (Sandbox Code Playgroud)

c# datagridview gridview-sorting winforms

3
推荐指数
1
解决办法
3407
查看次数